Skip to content

Commit c0475a6

Browse files
Allow mouse scroll wheel to change size/width of tool (#223)
* Allow mouse scroll wheel to change size/width of tool * Disable the size changing when zoomed in or out * Update drawing_tools_group.py --------- Co-authored-by: Alexander Vanhee <160625516+AlexanderVanhee@users.noreply.github.com>
1 parent 9596622 commit c0475a6

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

gradia/ui/drawing_tools_group.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2025 Alexander Vanhee, tfuxu
1+
# Copyright (C) 2025 Alexander Vanhee, tfuxu, kjozsa
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -47,6 +47,7 @@ def __init__(self, **kwargs):
4747
self.current_tool_config: Optional[ToolConfig] = None
4848
self.current_tool_option: Optional[ToolOption] = None
4949
self._updating_ui = False
50+
self._scroll_accum = 0.0
5051

5152
self.connect("realize", self._on_realize)
5253

@@ -95,23 +96,38 @@ def _update_ui_for_tool(self, tool_config: ToolConfig, tool_option: ToolOption):
9596
self._updating_ui = False
9697

9798
def _on_scroll(self, controller, dx, dy):
99+
if self.current_tool_option is None:
100+
return Gdk.EVENT_PROPAGATE
101+
98102
modifiers = controller.get_current_event_state()
99-
if (modifiers & Gdk.ModifierType.SHIFT_MASK) and (modifiers & Gdk.ModifierType.CONTROL_MASK):
103+
is_zoomed = self.get_root().image_bin.get_zoom_level() != 1
104+
105+
should_adjust_size = (
106+
(modifiers & Gdk.ModifierType.SHIFT_MASK) and (modifiers & Gdk.ModifierType.CONTROL_MASK)
107+
) or not is_zoomed
108+
109+
if should_adjust_size:
100110
adjustment = self.size_scale.get_adjustment()
101-
min_value = adjustment.get_lower()
102-
max_value = adjustment.get_upper()
111+
min_value = int(adjustment.get_lower())
112+
max_value = int(adjustment.get_upper())
103113

104-
step = math.copysign(1, -dy) if -dy != 0 else 0
105-
if dy < 0:
106-
new_size = self.current_tool_option.size + step
107-
else:
108-
new_size = self.current_tool_option.size + step
114+
if dy == 0:
115+
return Gdk.EVENT_PROPAGATE
109116

110-
new_size = max(min_value, min(max_value, new_size))
117+
step_unit = 0.35
118+
self._scroll_accum += (-step_unit if dy > 0 else step_unit)
119+
delta = int(self._scroll_accum)
120+
121+
if delta != 0:
122+
self._scroll_accum -= delta
123+
new_size = self.current_tool_option.size + delta
124+
new_size = max(min_value, min(max_value, new_size))
125+
126+
if new_size != self.current_tool_option.size:
127+
self.current_tool_option.size = new_size
128+
self.size_scale.set_value(new_size)
129+
self.trigger_action()
111130

112-
self.current_tool_option.size = new_size
113-
self.size_scale.set_value(new_size)
114-
self.trigger_action()
115131
return Gdk.EVENT_STOP
116132

117133
return Gdk.EVENT_PROPAGATE
@@ -192,6 +208,3 @@ def trigger_action(self):
192208
data_json = self.current_tool_option.serialize()
193209
param = GLib.Variant('s', data_json)
194210
action.activate(param)
195-
196-
197-

0 commit comments

Comments
 (0)