Skip to content

Commit 715fcea

Browse files
Steve-xmhxarvic
authored andcommitted
Added handle_titlebar support on GTK backend (linebender#2230)
1 parent e8752f0 commit 715fcea

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

druid-shell/src/backend/gtk/window.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub(crate) struct WindowState {
174174
scale: Cell<Scale>,
175175
area: Cell<ScaledArea>,
176176
is_transparent: Cell<bool>,
177+
handle_titlebar: Cell<bool>,
177178
/// Used to determine whether to honor close requests from the system: we inhibit them unless
178179
/// this is true, and this gets set to true when our client requests a close.
179180
closing: Cell<bool>,
@@ -353,6 +354,7 @@ impl WindowBuilder {
353354
scale: Cell::new(scale),
354355
area: Cell::new(area),
355356
is_transparent: Cell::new(transparent),
357+
handle_titlebar: Cell::new(false),
356358
closing: Cell::new(false),
357359
drawing_area,
358360
surface: RefCell::new(None),
@@ -580,6 +582,10 @@ impl WindowBuilder {
580582
},
581583
);
582584
}
585+
if button.is_left() && state.handle_titlebar.replace(false) {
586+
let (root_x, root_y) = event.root();
587+
state.window.begin_move_drag(event.button() as i32, root_x as i32, root_y as i32, event.time());
588+
}
583589
}
584590
});
585591
}
@@ -604,6 +610,9 @@ impl WindowBuilder {
604610
wheel_delta: Vec2::ZERO
605611
},
606612
);
613+
if button.is_left() {
614+
state.handle_titlebar.set(false);
615+
}
607616
}
608617
});
609618
}
@@ -1092,8 +1101,10 @@ impl WindowHandle {
10921101
Restored
10931102
}
10941103

1095-
pub fn handle_titlebar(&self, _val: bool) {
1096-
warn!("WindowHandle::handle_titlebar is currently unimplemented for gtk.");
1104+
pub fn handle_titlebar(&self, val: bool) {
1105+
if let Some(state) = self.state.upgrade() {
1106+
state.handle_titlebar.set(val);
1107+
}
10971108
}
10981109

10991110
/// Close the window.

druid-shell/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl WindowHandle {
206206
/// because this refers to the current location of the mouse, you should probably call this
207207
/// function in response to every relevant [`WinHandler::mouse_move`].
208208
///
209-
/// This is currently only implemented on Windows.
209+
/// This is currently only implemented on Windows and GTK.
210210
pub fn handle_titlebar(&self, val: bool) {
211211
self.0.handle_titlebar(val);
212212
}

druid/examples/transparency.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,33 @@
1818
// On Windows platform, don't show a console when opening the app.
1919
#![windows_subsystem = "windows"]
2020

21-
use druid::kurbo::Circle;
2221
use druid::widget::prelude::*;
2322
use druid::widget::{Flex, Label, Painter, TextBox, WidgetExt};
23+
use druid::{kurbo::Circle, widget::Controller};
2424
use druid::{AppLauncher, Color, Lens, Rect, WindowDesc};
2525

2626
#[derive(Clone, Data, Lens)]
2727
struct HelloState {
2828
name: String,
2929
}
3030

31+
struct DragController;
32+
33+
impl<T, W: Widget<T>> Controller<T, W> for DragController {
34+
fn event(
35+
&mut self,
36+
_child: &mut W,
37+
ctx: &mut EventCtx,
38+
event: &Event,
39+
_data: &mut T,
40+
_env: &Env,
41+
) {
42+
if let Event::MouseMove(_) = event {
43+
ctx.window().handle_titlebar(true);
44+
}
45+
}
46+
}
47+
3148
pub fn main() {
3249
let window = WindowDesc::new(build_root_widget())
3350
.show_titlebar(false)
@@ -81,7 +98,7 @@ fn build_root_widget() -> impl Widget<HelloState> {
8198
.with_text_size(32.0);
8299

83100
Flex::column()
84-
.with_flex_child(circle_and_rects.expand(), 10.0)
101+
.with_flex_child(circle_and_rects.expand().controller(DragController), 10.0)
85102
.with_spacer(4.0)
86103
.with_child(textbox)
87104
.with_spacer(4.0)

0 commit comments

Comments
 (0)