More drag/resize handle work

This commit is contained in:
itdominator 2023-04-20 23:03:27 -05:00
parent 50a537f572
commit 8495df5a39
2 changed files with 79 additions and 57 deletions

View File

@ -6,16 +6,25 @@ gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
# Application imports
class BodyGrid(Gtk.Grid):
def __init__(self, gdk_window):
def __init__(self, window, gdk_window):
super(BodyGrid, self).__init__()
self._gdk_window = gdk_window
self.is_dragging = False
self._window = window
self._is_dragging = False
self._update_block = False
self._drag_start_x = 0
self._drag_start_y = 0
self._current_x, \
self._current_y = window.get_position()
self._w1 = 0.0
self._h1 = 0.0
self._setup_styling()
self._setup_signals()
@ -39,60 +48,40 @@ class BodyGrid(Gtk.Grid):
def _load_widgets(self):
top_left = Gtk.Button("")
top_right = Gtk.Button("")
bottom_left = Gtk.Button("")
drag_button = Gtk.Button("")
bottom_right = Gtk.Button("")
box = Gtk.Box()
# box = Gtk.Box()
box2 = Gtk.Box()
box3 = Gtk.Box()
ctx1 = top_left.get_style_context()
ctx1.add_class("expand-button")
ctx2 = top_right.get_style_context()
ctx = drag_button.get_style_context()
ctx.add_class("expand-button")
ctx2 = bottom_right.get_style_context()
ctx2.add_class("expand-button")
ctx3 = bottom_left.get_style_context()
ctx3.add_class("expand-button")
ctx4 = bottom_right.get_style_context()
ctx4.add_class("expand-button")
width, height = 1, 1
row, col = 1, 1
self.attach(top_left, col, row, width, height)
row, col = 1, 5
self.attach(top_right, col, row, width, height)
row, col = 5, 1
self.attach(bottom_left, col, row, width, height)
row, col = 5, 5
self.attach(bottom_right, col, row, width, height)
row, col = 1, 2
self.attach(box, col, row, 1, 1)
self.attach(drag_button, col, row, 5, 1)
row, col = 2, 1
self.attach(box2, col, row, 4, 2)
row, col = 5, 2
self.attach(box3, col, row, 1, 1)
self.attach(box2, col, row, 5, 3)
row, col = 5, 1
self.attach(box3, col, row, 4, 1)
row, col = 5, 5
self.attach(bottom_right, col, row, 1, 1)
box.set_vexpand(True)
box.set_hexpand(True)
drag_button.set_vexpand(True)
drag_button.set_hexpand(True)
box2.set_vexpand(True)
box2.set_hexpand(True)
box3.set_vexpand(True)
box3.set_hexpand(True)
top_left.connect("button-press-event", self._press_event)
top_right.connect("button-press-event", self._press_event)
bottom_left.connect("button-press-event", self._press_event)
drag_button.connect("button-press-event", self._press_event)
drag_button.connect("motion-notify-event", self._move_motion_event)
drag_button.connect("button-release-event", self._release_event)
bottom_right.connect("button-press-event", self._press_event)
top_left.connect("motion-notify-event", self._resize_motion_event_tl)
top_right.connect("motion-notify-event", self._resize_motion_event_tr)
bottom_left.connect("motion-notify-event", self._resize_motion_event_bl)
bottom_right.connect("motion-notify-event", self._resize_motion_event_br)
top_left.connect("button-release-event", self._release_event)
top_right.connect("button-release-event", self._release_event)
bottom_left.connect("button-release-event", self._release_event)
bottom_right.connect("motion-notify-event", self._resize_motion_event)
bottom_right.connect("button-release-event", self._release_event)
def _press_event(self, widget = None, eve = None):
@ -100,27 +89,59 @@ class BodyGrid(Gtk.Grid):
cursor = Gdk.Cursor(Gdk.CursorType.CROSSHAIR)
window.get_window().set_cursor(cursor)
self.is_dragging = True
self._is_dragging = True
self._drag_start_x = eve.x
self._drag_start_y = eve.y
self._w1 = self._window.get_size()[0] # Ref window width
self._h1 = self._window.get_size()[1] # Ref window height
def _resize_motion_event_tl(self, widget = None, eve = None):
if self.is_dragging:
self._gdk_window.begin_resize_drag(Gdk.WindowEdge.NORTH_WEST, 1, eve.x_root, eve.y_root, eve.time)
def _resize_motion_event(self, widget = None, eve = None):
if self._update_block:
return
def _resize_motion_event_tr(self, widget = None, eve = None):
if self.is_dragging:
self._gdk_window.begin_resize_drag(Gdk.WindowEdge.NORTH_EAST, 1, eve.x_root, eve.y_root, eve.time)
x1 = self._drag_start_x
y1 = self._drag_start_y
x2 = eve.x
y2 = eve.y
w = 0
h = 0
def _resize_motion_event_bl(self, widget = None, eve = None):
if self.is_dragging:
self._gdk_window.begin_resize_drag(Gdk.WindowEdge.SOUTH_WEST, 1, eve.x_root, eve.y_root, eve.time)
if x2 > x1: # Is growing
w = self._w1 + (x2 - x1)
else: # Is shrinking
w = self._w1 - (x1 - x2)
def _resize_motion_event_br(self, widget = None, eve = None):
if self.is_dragging:
self._gdk_window.begin_resize_drag(Gdk.WindowEdge.SOUTH_EAST, 1, eve.x_root, eve.y_root, eve.time)
if y2 > y1: # Is growing
h = self._h1 + (y2 - y1)
else: # Is shrinking
h = self._h1 - (y1 - y2)
self._update_block = True
self._window.resize(w, h)
self._update_block = False
def _move_motion_event(self, widget = None, eve = None):
if self._update_block:
self._drag_start_x = eve.x
self._drag_start_y = eve.y
return
if self._is_dragging:
offset_x = eve.x - self._drag_start_x
offset_y = eve.y - self._drag_start_y
self._current_x += offset_x
self._current_y += offset_y
self._update_block = True
self._window.move(self._current_x, self._current_y)
self._update_block = False
def _release_event(self, widget = None, eve = None):
window = self.get_parent()
watch_cursor = Gdk.Cursor(Gdk.CursorType.ARROW)
window.get_window().set_cursor(watch_cursor)
self.is_dragging = False
self._is_dragging = False
self._drag_start_x = 0
self._drag_start_y = 0

View File

@ -31,6 +31,7 @@ class RegionWindow(Gtk.Window):
self.set_resizable(True)
self.set_skip_pager_hint(True)
self.set_skip_taskbar_hint(True)
self.set_has_resize_grip(True)
def _setup_signals(self):
...
@ -40,7 +41,7 @@ class RegionWindow(Gtk.Window):
def _load_widgets(self):
gdk_window = self.get_screen().get_root_window()
self.add( BodyGrid(gdk_window) )
self.add( BodyGrid(self, gdk_window) )
def _set_window_data(self) -> None:
screen = self.get_screen()