Skip to content

Commit

Permalink
#127 reposition traffic lights
Browse files Browse the repository at this point in the history
  • Loading branch information
neil authored and neil committed Jan 9, 2023
1 parent 1e802d8 commit bc9ea6e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/gui/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions modules/gui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.0", features = ["fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "http-all", "path-all", "shell-all", "window-all"] }
uuid = "1.2.1"
futures = "0.3"
objc = "0.2.7"
cocoa = "0.24.1"

[features]
# by default Tauri runs in production mode
Expand Down
14 changes: 14 additions & 0 deletions modules/gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@

mod handlers;

#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;

use tauri::{Manager, WindowEvent};
use window_ext::WindowExt;
mod window_ext;

fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
handlers::packages::install_package,
])
.setup(|app| {
let win = app.get_window("main").unwrap();
// win.set_transparent_titlebar(true);
// win.position_traffic_lights(30.0, 30.0);
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
61 changes: 61 additions & 0 deletions modules/gui/src-tauri/src/window_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use tauri::{Runtime, Window};

pub trait WindowExt {
#[cfg(target_os = "macos")]
fn set_transparent_titlebar(&self, transparent: bool);
fn position_traffic_lights(&self, x: f64, y: f64);
}

impl<R: Runtime> WindowExt for Window<R> {
#[cfg(target_os = "macos")]
fn set_transparent_titlebar(&self, transparent: bool) {
use cocoa::appkit::{NSWindow, NSWindowTitleVisibility};

let window = self.ns_window().unwrap() as cocoa::base::id;

unsafe {
window.setTitleVisibility_(NSWindowTitleVisibility::NSWindowTitleHidden);

if transparent {
window.setTitlebarAppearsTransparent_(cocoa::base::YES);
} else {
window.setTitlebarAppearsTransparent_(cocoa::base::NO);
}
}
}

#[cfg(target_os = "macos")]
fn position_traffic_lights(&self, x: f64, y: f64) {
use cocoa::appkit::{NSView, NSWindow, NSWindowButton};
use cocoa::foundation::NSRect;

let window = self.ns_window().unwrap() as cocoa::base::id;

unsafe {
let close = window.standardWindowButton_(NSWindowButton::NSWindowCloseButton);
let miniaturize =
window.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
let zoom = window.standardWindowButton_(NSWindowButton::NSWindowZoomButton);

let title_bar_container_view = close.superview().superview();

let close_rect: NSRect = msg_send![close, frame];
let button_height = close_rect.size.height;

let title_bar_frame_height = button_height + y;
let mut title_bar_rect = NSView::frame(title_bar_container_view);
title_bar_rect.size.height = title_bar_frame_height;
title_bar_rect.origin.y = NSView::frame(window).size.height - title_bar_frame_height;
let _: () = msg_send![title_bar_container_view, setFrame: title_bar_rect];

let window_buttons = vec![close, miniaturize, zoom];
let space_between = NSView::frame(miniaturize).origin.x - NSView::frame(close).origin.x;

for (i, button) in window_buttons.into_iter().enumerate() {
let mut rect: NSRect = NSView::frame(button);
rect.origin.x = x + (i as f64 * space_between);
button.setFrameOrigin(rect.origin);
}
}
}
}

0 comments on commit bc9ea6e

Please sign in to comment.