Coverage for src/robotide/editor/popupwindow.py: 80%

96 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2008-2015 Nokia Networks 

2# Copyright 2016- Robot Framework Foundation 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16import wx 1hi

17from ..context import POPUP_BACKGROUND, POPUP_FOREGROUND, IS_WINDOWS, IS_MAC 1hi

18from ..widgets import VerticalSizer, HtmlWindow, HtmlDialog 1hi

19 

20 

21class _PopupWindowBase(wx.Frame): 1hi

22 

23 def __init__(self, size, detachable=True, autohide=False, color_background=POPUP_BACKGROUND, 1hi

24 color_foreground=POPUP_FOREGROUND): 

25 self._current_details = None 1cdefgba

26 self._details = None 1cdefgba

27 self._detached_title = None 1cdefgba

28 self.color_background = color_background 1cdefgba

29 self.color_foreground = color_foreground 1cdefgba

30 self.panel = self._create_ui(size) 1cdefgba

31 if autohide: 1cdefgba

32 self._set_auto_hiding() 1ba

33 if detachable: 1cdefgba

34 self._set_detachable() 1cdefga

35 self.SetSize(size) 1cdefgba

36 

37 def _create_ui(self, size): 1hi

38 panel = wx.MiniFrame(self) 1cdefgba

39 # DEBUG: Make this colour dependent on colours cycle or by Library 

40 panel.SetBackgroundColour(self.color_background) 1cdefgba

41 panel.SetForegroundColour(self.color_foreground) 1cdefgba

42 szr = VerticalSizer() 1cdefgba

43 self._details = HtmlWindow(self, size=size) 1cdefgba

44 szr.add_expanding(self._details) 1cdefgba

45 # DEBUG: Grid Editor was broken on wxPython 4.2.0 with the SetSizer 

46 # panel.SetSizerAndFit(szr) 

47 return panel 1cdefgba

48 

49 def _set_detachable(self): 1hi

50 self._details.Bind(wx.EVT_LEFT_UP, self._detach) 1cdefga

51 self._details.Bind(wx.EVT_LEFT_DCLICK, self._detach) # DEBUG add double-click 1cdefga

52 

53 def _detach(self, event): 1hi

54 self.hide() 1a

55 dlg = HtmlDialog(self._detached_title, self._current_details) 1a

56 dlg.SetPosition((wx.GetMouseState().x, wx.GetMouseState().y)) 1a

57 dlg.Show() 1a

58 event.Skip() 1a

59 

60 def show_at(self, position): 1hi

61 if self.GetPosition() != position: 61 ↛ 63line 61 didn't jump to line 63 because the condition on line 61 was always true1ba

62 self.SetPosition(position) 1ba

63 if not self.IsShown(): 63 ↛ exitline 63 didn't return from function 'show_at' because the condition on line 63 was always true1ba

64 self.Show() 1ba

65 

66 def hide(self, event=None): 1hi

67 __ = event 1cdefgba

68 self.Show(False) 1cdefgba

69 

70 @property 1hi

71 def screen_position(self): 1hi

72 return self.ScreenPosition 1a

73 

74 @property 1hi

75 def pw_size(self): 1hi

76 return self.Size 1a

77 

78 def set_content(self, content, title=None): 1hi

79 if isinstance(self.color_background, tuple): 79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true1ba

80 bgcolor = '#' + ''.join(hex(item)[2:] for item in self.color_background) 

81 else: 

82 bgcolor = self.color_background 1ba

83 if isinstance(self.color_foreground, tuple): 83 ↛ 84line 83 didn't jump to line 84 because the condition on line 83 was never true1ba

84 fgcolor = '#' + ''.join(hex(item)[2:] for item in self.color_foreground) 

85 else: 

86 fgcolor = self.color_foreground 1ba

87 if content.startswith('<table>'): 87 ↛ 88line 87 didn't jump to line 88 because the condition on line 87 was never true1ba

88 new_content = content.replace("<table>", f'<div><font color="{fgcolor}"><table>')\ 

89 .replace("</table>", "</table></font></div>") 

90 else: 

91 new_content = f'<p><font color="{fgcolor}">' + content + '</font></p>' 1ba

92 self._current_details = '<body bgcolor=%s style="color:%s;">%s</body>' % (bgcolor, fgcolor, new_content) 1ba

93 self._details.SetPage(self._current_details) 1ba

94 self._detached_title = title 1ba

95 

96 def _set_auto_hiding(self): 1hi

97 raise NotImplementedError 

98 

99 

100class RidePopupWindow(wx.PopupWindow, _PopupWindowBase): 1hi

101 

102 def __init__(self, parent, size): 1hi

103 wx.PopupWindow.__init__(self, parent, flags=wx.CAPTION | wx.RESIZE_BORDER | wx.DEFAULT_FRAME_STYLE) 

104 self.SetSize(size) 

105 

106 def _set_auto_hiding(self): 1hi

107 # EVT_LEAVE is triggered on different components on different OSes. 

108 component_to_hide = self.panel if IS_WINDOWS else self 

109 component_to_hide.Bind(wx.EVT_LEAVE_WINDOW, self.hide) 

110 

111 

112class HtmlPopupWindow(RidePopupWindow): 1hi

113 

114 def __init__(self, parent, size, detachable=True, autohide=False): 1hi

115 from ..preferences import RideSettings 

116 _settings = RideSettings() 

117 self.general_settings = _settings['General'] 

118 self.color_background_help = self.general_settings['background help'] 

119 self.color_foreground_text = self.general_settings['foreground text'] 

120 RidePopupWindow.__init__(self, parent, size) 

121 _PopupWindowBase.__init__(self, size, detachable, autohide, color_background=self.color_background_help, 

122 color_foreground=self.color_foreground_text) 

123 

124 

125# DEBUG: See if we need to have Mac specific window 

126class MacRidePopupWindow(wx.MiniFrame, _PopupWindowBase): 1hi

127 

128 def __init__(self, parent, size, detachable=True, autohide=False): 1hi

129 wx.MiniFrame.__init__(self, parent, style=wx.SIMPLE_BORDER | wx.RESIZE_BORDER) 1cdefgba

130 # set Left to Right direction (while we don't have localization) 

131 self.SetLayoutDirection(wx.Layout_LeftToRight) 1cdefgba

132 from ..preferences import RideSettings 1cdefgba

133 _settings = RideSettings() 1cdefgba

134 self.general_settings = _settings['General'] 1cdefgba

135 self.color_background_help = self.general_settings['background help'] 1cdefgba

136 self.color_foreground_text = self.general_settings['foreground text'] 1cdefgba

137 _PopupWindowBase.__init__(self, size, detachable, autohide, color_background=self.color_background_help, 1cdefgba

138 color_foreground=self.color_foreground_text) 

139 self.hide() 1cdefgba

140 

141 def _set_auto_hiding(self): 1hi

142 self._details.Bind(wx.EVT_MOTION, lambda evt: self.hide()) 1ba

143 

144 def on_key(self, *params): 1hi

145 """ In the event we need to process key events""" 

146 pass 

147 

148 

149if IS_MAC: 149 ↛ 151line 149 didn't jump to line 151 because the condition on line 149 was always true1hi

150 RidePopupWindow = HtmlPopupWindow = MacRidePopupWindow 1hi

151del MacRidePopupWindow 1hi