Skip to content

Commit

Permalink
style(window): code format linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamoca42 authored and gitbutler-client committed Sep 22, 2024
1 parent 3f90ce5 commit f044581
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 136 deletions.
74 changes: 37 additions & 37 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,21 +428,21 @@ impl<T: 'static> EventLoop<T> {
if is_wayland {
window.connect_button_press_event(move |window, event| {
if event.button() == LMB {
let (x, y) = event.root();
let (window_x, window_y) = window.position();
let (window_x, window_y) = (window_x as f64, window_y as f64);
let border_width = window.border_width() as f64;
let (window_width, _) = window.size();
let window_width = window_width as f64;
if x > window_x + border_width &&
x < window_x + window_width - border_width &&
y > window_y + border_width &&
y < window_y + border_width + TITLEBAR_HEIGHT
let (x, y) = event.root();
let (window_x, window_y) = window.position();
let (window_x, window_y) = (window_x as f64, window_y as f64);

let border_width = window.border_width() as f64;
let (window_width, _) = window.size();
let window_width = window_width as f64;

if x > window_x + border_width
&& x < window_x + window_width - border_width
&& y > window_y + border_width
&& y < window_y + border_width + TITLEBAR_HEIGHT
{
window.begin_move_drag(LMB as i32, x as i32, y as i32, event.time());
return glib::Propagation::Stop;
window.begin_move_drag(LMB as i32, x as i32, y as i32, event.time());
return glib::Propagation::Stop;
}
}
glib::Propagation::Proceed
Expand Down Expand Up @@ -491,31 +491,31 @@ impl<T: 'static> EventLoop<T> {
});
window.connect_button_press_event(move |window, event| {
if event.button() == LMB {
let (cx, cy) = event.root();
let (left, top) = window.position();
let (w, h) = window.size();
let (right, bottom) = (left + w, top + h);
let border = window.scale_factor() * 5;
let edge = crate::window::hit_test(
(left, top, right, bottom),
cx as _,
cy as _,
border,
border,
)
.map(|d| d.to_gtk_edge())
// we return `WindowEdge::__Unknown` to be ignored later.
// we must return 8 or bigger, otherwise it will be the same as one of the other 7 variants of `WindowEdge` enum.
.unwrap_or(WindowEdge::__Unknown(8));
// Ignore the `__Unknown` variant so the window receives the click correctly if it is not on the edges.
match edge {
WindowEdge::__Unknown(_) => (),
_ => {
// FIXME: calling `window.begin_resize_drag` uses the default cursor, it should show a resizing cursor instead
window.begin_resize_drag(edge, LMB as i32, cx as i32, cy as i32, event.time())
}
let (cx, cy) = event.root();
let (left, top) = window.position();
let (w, h) = window.size();
let (right, bottom) = (left + w, top + h);
let border = window.scale_factor() * 5;
let edge = crate::window::hit_test(
(left, top, right, bottom),
cx as _,
cy as _,
border,
border,
)
.map(|d| d.to_gtk_edge())
// we return `WindowEdge::__Unknown` to be ignored later.
// we must return 8 or bigger, otherwise it will be the same as one of the other 7 variants of `WindowEdge` enum.
.unwrap_or(WindowEdge::__Unknown(8));
// Ignore the `__Unknown` variant so the window receives the click correctly if it is not on the edges.
match edge {
WindowEdge::__Unknown(_) => (),
_ => {
// FIXME: calling `window.begin_resize_drag` uses the default cursor, it should show a resizing cursor instead
window.begin_resize_drag(edge, LMB as i32, cx as i32, cy as i32, event.time())
}
}
}

glib::Propagation::Proceed
});
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ mod util;
mod window;

pub mod taskbar;
pub mod x11;
pub mod wayland;
pub mod x11;

pub use self::keycode::{keycode_from_scancode, keycode_to_scancode};
pub(crate) use event_loop::PlatformSpecificEventLoopAttributes;
Expand Down
188 changes: 92 additions & 96 deletions src/platform_impl/linux/wayland/display.rs
Original file line number Diff line number Diff line change
@@ -1,107 +1,103 @@
use gtk::{
prelude::*,
HeaderBar,
Button,
ToggleButton,
Image,
ApplicationWindow,
glib::{self},
gdk::{self},
gdk::{self},
glib::{self},
prelude::*,
ApplicationWindow, Button, HeaderBar, Image, ToggleButton,
};
pub struct WlHeader;

impl WlHeader {
pub fn setup(window: &ApplicationWindow) {
let header = HeaderBar::new();
header.set_show_close_button(false);
pub fn setup(window: &ApplicationWindow) {
let header = HeaderBar::new();
header.set_show_close_button(false);

let close_button = Self::create_header_button("window-close-symbolic");
let maximize_button = Self::create_header_toggle_button("window-maximize-symbolic");
let minimize_button = Self::create_header_button("window-minimize-symbolic");
let close_button = Self::create_header_button("window-close-symbolic");
let maximize_button = Self::create_header_toggle_button("window-maximize-symbolic");
let minimize_button = Self::create_header_button("window-minimize-symbolic");

Self::connect_close_button(close_button.clone(), window);
Self::connect_maximize_button(maximize_button.clone(), window);
Self::connect_minimize_button(minimize_button.clone(), window);
Self::connect_close_button(close_button.clone(), window);
Self::connect_maximize_button(maximize_button.clone(), window);
Self::connect_minimize_button(minimize_button.clone(), window);

header.pack_end(&close_button);
header.pack_end(&maximize_button);
header.pack_end(&minimize_button);

window.set_titlebar(Some(&header));
}

fn create_header_button(icon_name: &str) -> Button {
let button = Button::new();
let icon = Image::from_icon_name(Some(icon_name), gtk::IconSize::Button);
button.set_image(Some(&icon));
button
}

fn create_header_toggle_button(icon_name: &str) -> ToggleButton {
let button = ToggleButton::new();
let icon = Image::from_icon_name(Some(icon_name), gtk::IconSize::Button);
button.set_image(Some(&icon));
button
}

fn connect_close_button(button: Button, window: &ApplicationWindow) {
let window_weak = window.downgrade();
button.connect_clicked(move |_| {
if let Some(window) = window_weak.upgrade() {
window.close();
}
});
header.pack_end(&close_button);
header.pack_end(&maximize_button);
header.pack_end(&minimize_button);

window.set_titlebar(Some(&header));
}

fn create_header_button(icon_name: &str) -> Button {
let button = Button::new();
let icon = Image::from_icon_name(Some(icon_name), gtk::IconSize::Button);
button.set_image(Some(&icon));
button
}

fn create_header_toggle_button(icon_name: &str) -> ToggleButton {
let button = ToggleButton::new();
let icon = Image::from_icon_name(Some(icon_name), gtk::IconSize::Button);
button.set_image(Some(&icon));
button
}

fn connect_close_button(button: Button, window: &ApplicationWindow) {
let window_weak = window.downgrade();
button.connect_clicked(move |_| {
if let Some(window) = window_weak.upgrade() {
window.close();
}
});
}

fn connect_maximize_button(button: ToggleButton, window: &ApplicationWindow) {
let window_weak = window.downgrade();
// initial state
if let Some(window) = window_weak.upgrade() {
button.set_active(window.is_maximized());
button.set_sensitive(window.is_resizable());
}

fn connect_maximize_button(button: ToggleButton, window: &ApplicationWindow) {
let window_weak = window.downgrade();
// initial state
if let Some(window) = window_weak.upgrade() {
button.set_active(window.is_maximized());
button.set_sensitive(window.is_resizable());

let button_weak = button.downgrade();
window.connect_is_maximized_notify(move |window| {
if let Some(button) = button_weak.upgrade() {
button.set_active(window.is_maximized());
}
});

let button_weak = button.downgrade();
window.connect_resizable_notify(move |window| {
if let Some(button) = button_weak.upgrade() {
button.set_sensitive(window.is_resizable());
}
});

//click event
button.connect_toggled(move |button| {
if let Some(window) = window_weak.upgrade() {
if button.is_active() && window.is_resizable() {
window.maximize();
} else {
window.unmaximize();
}

let button_weak = button.downgrade();
window.connect_is_maximized_notify(move |window| {
if let Some(button) = button_weak.upgrade() {
button.set_active(window.is_maximized());
}
});

let button_weak = button.downgrade();
window.connect_resizable_notify(move |window| {
if let Some(button) = button_weak.upgrade() {
button.set_sensitive(window.is_resizable());
}
});

//click event
button.connect_toggled(move |button| {
if let Some(window) = window_weak.upgrade() {
if button.is_active() && window.is_resizable() {
window.maximize();
} else {
window.unmaximize();
}
}
});

// Prevent space key activation in header
button.connect_key_press_event(move |_, event_key| {
if event_key.keyval() == gdk::keys::constants::space {
glib::Propagation::Stop
} else {
glib::Propagation::Proceed
}
});
}

fn connect_minimize_button(button: Button, window: &ApplicationWindow) {
let window_weak = window.downgrade();
button.connect_clicked(move |_| {
if let Some(window) = window_weak.upgrade() {
window.iconify();
}
});
}
}
});

// Prevent space key activation in header
button.connect_key_press_event(move |_, event_key| {
if event_key.keyval() == gdk::keys::constants::space {
glib::Propagation::Stop
} else {
glib::Propagation::Proceed
}
});
}

fn connect_minimize_button(button: Button, window: &ApplicationWindow) {
let window_weak = window.downgrade();
button.connect_clicked(move |_| {
if let Some(window) = window_weak.upgrade() {
window.iconify();
}
});
}
}
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

pub mod display;
pub mod display;
2 changes: 1 addition & 1 deletion src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use crate::{
error::{ExternalError, NotSupportedError, OsError as RootOsError},
icon::Icon,
monitor::MonitorHandle as RootMonitorHandle,
platform_impl::wayland::display::WlHeader,
window::{
CursorIcon, Fullscreen, ProgressBarState, ResizeDirection, Theme, UserAttentionType,
WindowAttributes, WindowSizeConstraints,
},
platform_impl::wayland::display::WlHeader,
};

use super::{
Expand Down

0 comments on commit f044581

Please sign in to comment.