Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions enable/abstract_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ def __init__(self, **traits):

def _component_changed(self, old, new):
if old is not None:
old.on_trait_change(
self.component_bounds_changed, "bounds", remove=True
old.observe(
self.component_bounds_updated, "bounds", remove=True
)
old.window = None

Expand All @@ -218,28 +218,28 @@ def _component_changed(self, old, new):
# If possible, size the new component according to the size of the
# toolkit control
size = self._get_control_size()
if (size is not None) and hasattr(self.component, "bounds"):
new.on_trait_change(self.component_bounds_changed, "bounds")
pix_scale = self.base_pixel_scale
if getattr(self.component, "fit_window", False):
self.component.outer_position = [0, 0]
self.component.outer_bounds = [
size[0] / pix_scale, size[1] / pix_scale
]
elif hasattr(self.component, "resizable"):
if "h" in self.component.resizable:
self.component.outer_x = 0
self.component.outer_width = size[0] / pix_scale
if "v" in self.component.resizable:
self.component.outer_y = 0
self.component.outer_height = size[1] / pix_scale
if new is not None:
new.observe(self.component_bounds_updated, "bounds")
if size is not None:
pix_scale = self.base_pixel_scale
if getattr(self.component, "fit_window", False):
self.component.outer_position = [0, 0]
self.component.outer_bounds = [
size[0] / pix_scale, size[1] / pix_scale
]
elif hasattr(self.component, "resizable"):
if "h" in self.component.resizable:
self.component.outer_x = 0
self.component.outer_width = size[0] / pix_scale
if "v" in self.component.resizable:
self.component.outer_y = 0
self.component.outer_height = size[1] / pix_scale
self._update_region = None
self.redraw()

def component_bounds_changed(self, bounds):
def component_bounds_updated(self, event):
"""
Dynamic trait listener that handles our component changing its size;
bounds is a length-2 list of [width, height].
Dynamic trait listener that handles our component changing its size.
"""
self.invalidate_draw()
pass
Expand Down
12 changes: 4 additions & 8 deletions enable/drawing/drawing_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,14 @@ def add_button(self, *buttons):

def _canvas_changed(self, old, new):
if old:
old.on_trait_change(
self._canvas_bounds_changed, "bounds", remove=True
)
old.on_trait_change(
self._canvas_bounds_changed, "bounds_items", remove=True
old.observe(
self._canvas_bounds_updated, "bounds.items", remove=True
)

if new:
new.on_trait_change(self._canvas_bounds_changed, "bounds")
new.on_trait_change(self._canvas_bounds_changed, "bounds_items")
new.observe(self._canvas_bounds_updated, "bounds.items")

def _canvas_bounds_changed(self):
def _canvas_bounds_updated(self, event):
self.width = self.canvas.width
self.y = self.canvas.height - self.height

Expand Down
4 changes: 2 additions & 2 deletions enable/examples/demo/enable/compass_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def _create_component(self):
container = OverlayContainer()
container.add(compass)

compass.on_trait_change(self._arrow_printer, "clicked")
compass.observe(self._arrow_printer, "clicked")
self.compass = compass
return container

def _arrow_printer(self):
def _arrow_printer(self, event):
print("Clicked:", self.compass.clicked)


Expand Down
13 changes: 6 additions & 7 deletions enable/examples/demo/enable/scrollbar_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _create_component(self):
range=(0, 100.0, 10.0, 1.0),
enabled=True,
)
vscroll.on_trait_change(self._update_vscroll, "scroll_position")
vscroll.observe(self._update_vscroll, "scroll_position")

hscroll = NativeScrollBar(
orientation="horizontal",
Expand All @@ -40,31 +40,30 @@ def _create_component(self):
range=(0, 100.0, 10.0, 1.0),
enabled=True,
)
hscroll.on_trait_change(self._update_hscroll, "scroll_position")
hscroll.observe(self._update_hscroll, "scroll_position")

container = Container(
bounds=[200, 200], border_visible=True, padding=15
)
container.add(label, hscroll, vscroll)
container.on_trait_change(self._update_layout, "bounds")
container.on_trait_change(self._update_layout, "bounds_items")
container.observe(self._update_layout, "bounds.items")

self.label = label
self.hscroll = hscroll
self.vscroll = vscroll
return container

def _update_hscroll(self):
def _update_hscroll(self, event):
text = self.label.text.split("\n")
text[0] = "h: " + str(self.hscroll.scroll_position)
self.label.text = "\n".join(text)

def _update_vscroll(self):
def _update_vscroll(self, event):
text = self.label.text.split("\n")
text[1] = "v: " + str(self.vscroll.scroll_position)
self.label.text = "\n".join(text)

def _update_layout(self):
def _update_layout(self, event):
self.vscroll._widget_moved = True
self.hscroll._widget_moved = True

Expand Down
4 changes: 2 additions & 2 deletions enable/examples/demo/enable/slider_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def _create_component(self):
container = OverlayContainer()
container.add(slider)

slider.on_trait_change(self.val_changed, "value")
slider.observe(self.val_changed, "value")
self.slider = slider
return container

def val_changed(self):
def val_changed(self, event):
print(self.slider.value)


Expand Down
11 changes: 6 additions & 5 deletions enable/examples/demo/enable/tools/button_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class SelectableBox(Box):

selected_color = ColorTrait("green")

def select(self, selected):
def select(self, event):
selected = event.new
self.selected = selected

def _selected_changed(self):
Expand Down Expand Up @@ -68,7 +69,7 @@ class MyFrame(DemoFrame):

"""

def button_clicked(self):
def button_clicked(self, event):
print("clicked")

# -------------------------------------------------------------------------
Expand All @@ -86,8 +87,8 @@ def _create_component(self):
push_button_box.tools.append(push_button_tool)

# print when box clicked, change color when button down
push_button_tool.on_trait_change(self.button_clicked, "clicked")
push_button_tool.on_trait_change(push_button_box.select, "down")
push_button_tool.observe(self.button_clicked, "clicked")
push_button_tool.observe(push_button_box.select, "down")

# another box for a toggle button
toggle_box = SelectableBox(
Expand All @@ -102,7 +103,7 @@ def _create_component(self):
toggle_box.tools.append(toggle_button_tool)

# change color when checked down
toggle_button_tool.on_trait_change(toggle_box.select, "checked")
toggle_button_tool.observe(toggle_box.select, "checked")

container = Container(bounds=[600, 600])
container.add(push_button_box)
Expand Down
41 changes: 41 additions & 0 deletions enable/tests/test_abstract_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest
from unittest import mock

from enable.abstract_window import AbstractWindow
from enable.component import Component


class TestAbstractWindow(unittest.TestCase):

@mock.patch.object(AbstractWindow, "component_bounds_updated")
def test_component_bounds_updated(self, mock_method):
""" Make sure trait listener for changing component bounds gets set up.
"""

class TestWindow(AbstractWindow):
# needed to avoid a NotImplementedError, not under test
def _redraw(self):
pass

def _get_control_size(self):
# this happens in the wild
return None

thing = Component()

TestWindow(
parent=None,
component=thing,
)
thing.bounds = [13.0, 13.0]

self.assertTrue(mock_method.called)
11 changes: 7 additions & 4 deletions enable/text_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,16 @@ def _refresh_viewed_line(self, line):
def _acquire_focus(self, window):
self._draw_cursor = True
window.focus_owner = self
window.on_trait_change(self._focus_owner_changed, "focus_owner")
window.observe(self._window_focus_owner_updated, "focus_owner")
self.request_redraw()

def _focus_owner_changed(self, obj, name, old, new):
def _window_focus_owner_updated(self, event):
obj = event.object
old = event.old
new = event.new
if old == self and new != self:
obj.on_trait_change(
self._focus_owner_changed, "focus_owner", remove=True
obj.observe(
self._window_focus_owner_updated, "focus_owner", remove=True
)
self._draw_cursor = False
self.request_redraw()
Expand Down
4 changes: 2 additions & 2 deletions enable/trait_defs/ui/wx/enable_rgba_color_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def init(self, parent):
self.control = window.control
self._picker = picker
self.control.SetSize((picker.min_width, picker.min_height))
picker.on_trait_change(self.popup_editor, "clicked", dispatch="ui")
picker.on_trait_change(self.update_object, "color", dispatch="ui")
picker.observe(self.popup_editor, "clicked", dispatch="ui")
picker.observe(self.update_object, "color", dispatch="ui")

# -------------------------------------------------------------------------
# Invokes the pop-up editor for an object trait:
Expand Down
2 changes: 1 addition & 1 deletion enable/trait_defs/ui/wx/rgba_color_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def init(self, parent):
self._swatch = window.component
self.control = window.control
self.control.SetSize((110, 20))
window.component.on_trait_change(
window.component.observe(
self.popup_editor, "left_up", dispatch="ui"
)

Expand Down