Skip to content

Commit

Permalink
Add Qt5 toolbar workaround using "win32" ToolbarWrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasily-v-ryabov committed Apr 4, 2021
1 parent 7139889 commit 547e4e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions pywinauto/controls/uia_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ def __init__(self, elem):
"""Initialize the control"""
super(ToolbarWrapper, self).__init__(elem)
self.win32_wrapper = None
if not self.children() and self.element_info.handle is not None:
if len(self.children()) <= 1 and self.element_info.handle is not None:
self.win32_wrapper = common_controls.ToolbarWrapper(self.element_info.handle)

@property
Expand All @@ -1181,21 +1181,30 @@ def texts(self):
def button_count(self):
"""Return a number of buttons on the ToolBar"""
if self.win32_wrapper is not None:
return self.win32_wrapper.button_count()
btn_count = self.win32_wrapper.button_count()
if btn_count:
return btn_count
return len(self.win32_wrapper.children())
else:
return len(self.children())

# ----------------------------------------------------------------
def buttons(self):
"""Return all available buttons"""
if self.win32_wrapper is not None:
btn_count = self.win32_wrapper.button_count()
cc = []
for btn_num in range(btn_count):
relative_point = self.win32_wrapper.get_button_rect(btn_num).mid_point()
button_coord_x, button_coord_y = self.client_to_screen(relative_point)
btn_elem_info = UIAElementInfo.from_point(button_coord_x, button_coord_y)
cc.append(uiawrapper.UIAWrapper(btn_elem_info))
btn_count = self.win32_wrapper.button_count()
if btn_count:
# MFC toolbar replies on TB_BUTTONCOUNT window message
for btn_num in range(btn_count):
relative_point = self.win32_wrapper.get_button_rect(btn_num).mid_point()
button_coord_x, button_coord_y = self.client_to_screen(relative_point)
btn_elem_info = UIAElementInfo.from_point(button_coord_x, button_coord_y)
cc.append(uiawrapper.UIAWrapper(btn_elem_info))
else:
# Qt5 toolbar doesn't reply on TB_BUTTONCOUNT window message
for btn in self.win32_wrapper.children():
cc.append(uiawrapper.UIAWrapper(UIAElementInfo(btn.handle)))
else:
cc = self.children()
return cc
Expand Down
4 changes: 2 additions & 2 deletions pywinauto/unittests/test_uiawrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ def tearDown(self):
"""Close the application after tests"""
self.app.kill()

def test_button_access(self):
def test_button_access_wpf(self):
"""Test getting access to buttons on Toolbar of WPF demo"""
# Read a second toolbar with buttons: "button1, button2"
tb = self.dlg.Toolbar2.find()
Expand Down Expand Up @@ -1912,7 +1912,7 @@ def tearDown(self):
self.menu_bar.move_mouse_input(coords=self.window_edge_point, absolute=False)
self.app.kill()

def test_button_access(self):
def test_button_access_mfc(self):
"""Test getting access to buttons on Toolbar for MFC demo"""
# Read a first toolbar with buttons: "File, View, Help"
self.assertEqual(self.menu_bar.button_count(), 4)
Expand Down

0 comments on commit 547e4e4

Please sign in to comment.