Skip to content

Commit

Permalink
change window_drag_area to handle_titlebar_area
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Aug 19, 2023
1 parent 28043fc commit 3c74df6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ taffy = "0.3.12"
unicode-segmentation = "1.10.0"
glazier = { git = "https://github.com/lapce/glazier", features = [
"serde",
], rev = "b41763b25b028963cdf8f95c4f36409f827805c3" }
], rev = "2e7f65fc6e7f41dfcd3802b96d44a0b62051cace" }
# glazier = { path = "../glazier", features = ["serde"] }
peniko = { git = "https://github.com/linebender/peniko", rev = "cafdac9a211a0fb2fec5656bd663d1ac770bcc81" }
crossbeam-channel = "0.5.6"
Expand Down
4 changes: 4 additions & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fn add_update_message(msg: UpdateMessage) {
});
}

pub fn toggle_window_maximized() {
add_update_message(UpdateMessage::ToggleWindowMaximized);
}

pub fn set_handle_titlebar(val: bool) {
add_update_message(UpdateMessage::HandleTitleBar(val));
}
Expand Down
19 changes: 18 additions & 1 deletion src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::window::WINDOWS;
use floem_reactive::{with_scope, Scope};
use floem_renderer::Renderer;
use glazier::kurbo::{Affine, Point, Rect, Size, Vec2};
use glazier::{FileDialogOptions, FileDialogToken, FileInfo, Scale, TimerToken, WinHandler};
use glazier::{
FileDialogOptions, FileDialogToken, FileInfo, Scale, TimerToken, WinHandler, WindowState,
};

use crate::menu::Menu;
use crate::{
Expand Down Expand Up @@ -171,6 +173,7 @@ pub enum UpdateMessage {
id: Id,
action: Box<dyn Fn()>,
},
ToggleWindowMaximized,
HandleTitleBar(bool),
SetWindowDelta(Vec2),
OpenFile {
Expand Down Expand Up @@ -537,6 +540,20 @@ impl<V: View> AppHandle<V> {
UpdateMessage::HandleTitleBar(val) => {
self.handle.handle_titlebar(val);
}
UpdateMessage::ToggleWindowMaximized => {
let window_state = self.handle.get_window_state();
match window_state {
glazier::WindowState::Maximized => {
self.handle.set_window_state(WindowState::Restored);
}
glazier::WindowState::Minimized => {
self.handle.set_window_state(WindowState::Maximized);
}
glazier::WindowState::Restored => {
self.handle.set_window_state(WindowState::Maximized);
}
}
}
UpdateMessage::SetWindowDelta(delta) => {
self.handle.set_position(self.handle.get_position() + delta);
}
Expand Down
54 changes: 21 additions & 33 deletions src/views/window_drag_area.rs → src/views/handle_titlebar_area.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use glazier::kurbo::{Point, Rect};
use glazier::kurbo::Rect;

use crate::{
action::{set_handle_titlebar, set_window_delta},
app_handle::ViewContext,
action::{set_handle_titlebar, toggle_window_maximized},
event::Event,
id::Id,
view::{ChangeFlags, View},
view::View,
ViewContext,
};

pub struct WindowDragArea<V: View> {
use super::Decorators;

pub struct HandleTitlebarArea<V: View> {
id: Id,
prev_pos: Option<Point>,
child: V,
}

pub fn window_drag_area<V: View>(child: impl FnOnce() -> V) -> WindowDragArea<V> {
pub fn handle_titlebar_area<V: View>(child: impl FnOnce() -> V) -> HandleTitlebarArea<V> {
let (id, child) = ViewContext::new_id_with_child(child);
WindowDragArea {
id,
child,
prev_pos: None,
}
HandleTitlebarArea { id, child }.on_double_click(|_| {
toggle_window_maximized();
true
})
}

impl<V: View> View for WindowDragArea<V> {
impl<V: View> View for HandleTitlebarArea<V> {
fn id(&self) -> Id {
self.id
}
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<V: View> View for WindowDragArea<V> {
_cx: &mut crate::context::UpdateCx,
_state: Box<dyn std::any::Any>,
) -> crate::view::ChangeFlags {
ChangeFlags::empty()
crate::view::ChangeFlags::empty()
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
Expand All @@ -76,33 +76,21 @@ impl<V: View> View for WindowDragArea<V> {
) -> bool {
if !self.child.event_main(cx, id_path, event.clone()) {
match &event {
Event::PointerDown(mouse_event) => {
if mouse_event.button.is_primary() {
self.prev_pos = Some(mouse_event.pos);
cx.update_active(self.id);
Event::PointerDown(pointer_event) => {
if pointer_event.button.is_primary() {
set_handle_titlebar(true);
}
true
}
Event::PointerUp(mouse_event) => {
if mouse_event.button.is_primary() {
Event::PointerUp(pointer_event) => {
if pointer_event.button.is_primary() {
set_handle_titlebar(false);
self.prev_pos = None;
}
true
}
Event::PointerMove(mouse_event) => {
if let Some(prev_pos) = self.prev_pos {
let position_diff = mouse_event.pos - prev_pos;
set_window_delta(position_diff);
return true;
}
false
}
_ => false,
_ => (),
}
} else {
false
} else {
true
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ pub use text_input::*;
mod empty;
pub use empty::*;

mod window_drag_area;
pub use window_drag_area::*;
mod handle_titlebar_area;
pub use handle_titlebar_area::*;

0 comments on commit 3c74df6

Please sign in to comment.