Skip to content

Commit

Permalink
Ability to start the app with --hidden (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
naps62 committed Nov 19, 2023
1 parent f4aae8b commit 1d9631b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
6 changes: 4 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set positional-arguments

alias d := dev
alias f := fix
alias l := lint
Expand All @@ -10,8 +12,8 @@ build:
yarn extension:build
cargo build

dev:
yarn run tauri dev --features ${IRON_FEATURES:-debug}
dev *args='':
yarn run tauri dev --features ${IRON_FEATURES:-debug} -- -- -- $@

fix:
cargo +nightly fmt --all
Expand Down
28 changes: 6 additions & 22 deletions bin/iron/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use std::path::PathBuf;
use iron_args::Args;
use iron_broadcast::UIMsg;
use iron_db::DB;
use iron_settings::Settings;
use iron_types::GlobalState;
#[cfg(target_os = "macos")]
use tauri::WindowEvent;
use tauri::{AppHandle, Builder, GlobalWindowEvent, Manager, Window, Wry};
use tauri::{AppHandle, Builder, GlobalWindowEvent, Manager};
use tauri_plugin_window_state::Builder as windowStatePlugin;

use crate::{
Expand Down Expand Up @@ -90,7 +88,10 @@ impl IronApp {
.expect("error while running tauri application");

init(&app, args).await?;
build_main_window(&app).await?;

if !args.hidden {
main_window_show(&app.handle()).await;
}

Ok(Self { app })
}
Expand Down Expand Up @@ -136,23 +137,6 @@ async fn init(app: &tauri::App, args: &Args) -> AppResult<()> {
Ok(())
}

async fn build_main_window(app: &tauri::App) -> tauri::Result<Window<Wry>> {
let onboarded = Settings::read().await.onboarded();
let url = if onboarded { "/" } else { "/onboarding" };

let builder = tauri::WindowBuilder::new(app, "main", tauri::WindowUrl::App(url.into()))
.fullscreen(false)
.resizable(true)
.inner_size(600.0, 800.0);

#[cfg(target_os = "macos")]
let builder = builder
.title_bar_style(tauri::TitleBarStyle::Overlay)
.hidden_title(true);

builder.build()
}

#[cfg(target_os = "macos")]
fn on_window_event(event: GlobalWindowEvent) {
if let WindowEvent::CloseRequested { api, .. } = event.event() {
Expand Down Expand Up @@ -187,7 +171,7 @@ async fn event_listener(handle: AppHandle) {
DialogClose(params) => dialogs::close(&handle, params),
DialogSend(params) => dialogs::send(&handle, params),

MainWindowShow => main_window_show(&handle),
MainWindowShow => main_window_show(&handle).await,
MainWindowHide => main_window_hide(&handle),
}
}
Expand Down
10 changes: 6 additions & 4 deletions bin/iron/src/system_tray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use tauri::{AppHandle, Manager, SystemTrayEvent};

use crate::utils::main_window_show;

pub(crate) fn build() -> tauri::SystemTray {
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayMenuItem};

Expand All @@ -24,10 +22,14 @@ pub(crate) fn event_handler(app: &AppHandle, event: SystemTrayEvent) {
MenuItemClick { id, .. } => match id.as_str() {
"quit" => app.exit(0),
"hide" => app.get_window("main").unwrap().hide().unwrap(),
"show" => main_window_show(app),
"show" => {
tokio::spawn(async { iron_broadcast::main_window_show().await });
}
_ => {}
},
DoubleClick { .. } => main_window_show(app),
DoubleClick { .. } => {
tokio::spawn(async { iron_broadcast::main_window_show().await });
}
_ => {}
}
}
23 changes: 19 additions & 4 deletions bin/iron/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
use iron_settings::Settings;
use iron_types::GlobalState;
use tauri::{AppHandle, Manager};

pub(crate) fn main_window_show(app: &AppHandle) {
pub(crate) async fn main_window_show(app: &AppHandle) {
if let Some(w) = app.get_window("main") {
w.show().unwrap()
} else {
tauri::WindowBuilder::new(app, "main", tauri::WindowUrl::App("index.html".into()))
.build()
.unwrap();
let app = app.clone();
let onboarded = Settings::read().await.onboarded();
let url = if onboarded { "/" } else { "/onboarding" };

let builder = tauri::WindowBuilder::new(&app, "main", tauri::WindowUrl::App(url.into()))
.fullscreen(false)
.resizable(true)
.visible(false)
.inner_size(600.0, 800.0);

#[cfg(target_os = "macos")]
let builder = builder
.title_bar_style(tauri::TitleBarStyle::Overlay)
.hidden_title(true);

builder.build().unwrap();
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/args/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub struct Args {

#[arg(long, default_value_t = 9002, env = "IRON_WS_PORT")]
pub ws_port: u16,

#[arg(long, default_value_t = false)]
pub hidden: bool,
}

0 comments on commit 1d9631b

Please sign in to comment.