Skip to content

Commit

Permalink
feat: add minimize on startup option
Browse files Browse the repository at this point in the history
Signed-off-by: Martichou <[email protected]>
  • Loading branch information
Martichou committed Aug 6, 2024
1 parent 768d4e4 commit e084c4b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/common/vue_lib/src/helper/ParamsHelper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface TauriVM {
version: string | null;
autostart: boolean;
realclose: boolean;
startminimized: boolean;
visibility: Visibility;
downloadPath: string | undefined;
hostname: string | undefined;
Expand Down
1 change: 1 addition & 0 deletions app/common/vue_lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const numberToVisibility: { [key: number]: Visibility } = {

export const autostartKey = "autostart";
export const realcloseKey = "realclose";
export const startminimizedKey = "startminimized";
export const visibilityKey = "visibility";
export const downloadPathKey = "download_path";
export const stateToDisplay: Array<Partial<State>> = ["ReceivedPairedKeyResult", "WaitingForUserConsent", "ReceivingFiles", "Disconnected",
Expand Down
14 changes: 13 additions & 1 deletion app/common/vue_lib/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Visibility } from '@martichou/core_lib/bindings/Visibility';
import { TauriVM } from './helper/ParamsHelper';
import { autostartKey, DisplayedItem, downloadPathKey, numberToVisibility, realcloseKey, stateToDisplay, visibilityKey, visibilityToNumber } from './types';
import { autostartKey, DisplayedItem, downloadPathKey, numberToVisibility, realcloseKey, startminimizedKey, stateToDisplay, visibilityKey, visibilityToNumber } from './types';
import { SendInfo } from '@martichou/core_lib/bindings/SendInfo';
import { ChannelMessage } from '@martichou/core_lib/bindings/ChannelMessage';
import { ChannelAction } from '@martichou/core_lib';
Expand Down Expand Up @@ -80,6 +80,16 @@ async function getRealclose(vm: TauriVM) {
vm.realclose = await vm.store.get(realcloseKey) ?? false;
}

async function setStartMinimized(vm: TauriVM, startminimized: boolean) {
await vm.store.set(startminimizedKey, startminimized);
await vm.store.save();
vm.startminimized = startminimized;
}

async function getStartMinimized(vm: TauriVM) {
vm.startminimized = await vm.store.get(startminimizedKey) ?? false;
}

async function setVisibility(vm: TauriVM, visibility: Visibility) {
await vm.invoke('change_visibility', { message: visibility });
await vm.store.set(visibilityKey, visibilityToNumber[visibility]);
Expand Down Expand Up @@ -205,6 +215,8 @@ const utils = {
setDownloadPath,
getDownloadPath,
getLatestVersion,
setStartMinimized,
getStartMinimized
};

export default utils;
12 changes: 10 additions & 2 deletions app/legacy/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::{Arc, Mutex};

use rqs_lib::channel::{ChannelDirection, ChannelMessage};
use rqs_lib::{EndpointInfo, SendInfo, State, Visibility, RQS};
use store::get_startminimized;
use tauri::{
AppHandle, CustomMenuItem, GlobalWindowEvent, Manager, SystemTray, SystemTrayEvent,
SystemTrayMenu, SystemTrayMenuItem,
Expand Down Expand Up @@ -115,11 +116,18 @@ async fn main() -> Result<(), anyhow::Error> {
.on_window_event(handle_window_event)
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app_handle, event| {
if let tauri::RunEvent::ExitRequested { .. } = event {
.run(|app_handle, event| match event {
tauri::RunEvent::Ready { .. } => {
trace!("RunEvent::Ready");
if get_startminimized(app_handle) {
app_handle.get_window("main").unwrap().hide().unwrap();
}
}
tauri::RunEvent::ExitRequested { .. } => {
trace!("RunEvent::ExitRequested");
kill_app(app_handle);
}
_ => {}
});

info!("Application stopped");
Expand Down
20 changes: 20 additions & 0 deletions app/legacy/src-tauri/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn init_default(app_handle: &AppHandle) {
)
.ok();
}

if !store.has("startminimized") {
store
.insert("startminimized".to_owned(), JsonValue::Bool(false))
.ok();
}
Ok(())
},
);
Expand Down Expand Up @@ -113,3 +119,17 @@ pub fn get_logging_level(app_handle: &AppHandle) -> Option<String> {
.ok()
.flatten()
}

pub fn get_startminimized(app_handle: &AppHandle) -> bool {
with_store(
app_handle.clone(),
app_handle.state(),
".settings.json",
|store| Ok(store.get("startminimized").and_then(|json| json.as_bool())),
)
.unwrap_or_else(|e| {
error!("get_startminimized: error: {}", e);
None
})
.unwrap_or(false)
}
8 changes: 8 additions & 0 deletions app/legacy/src/components/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<input type="checkbox" :checked="!realclose" class="checkbox focus:outline-none">
</label>
</div>
<div class="form-control hover:bg-gray-500 hover:bg-opacity-10 rounded-xl p-3">
<label class="cursor-pointer flex flex-row justify-between items-center" @click="setStartMinimized(vm, !startminimized)">
<span class="label-text">Start minimized</span>
<input type="checkbox" :checked="startminimized" class="checkbox focus:outline-none">
</label>
</div>
<div class="form-control hover:bg-gray-500 hover:bg-opacity-10 rounded-xl p-3">
<label class="cursor-pointer flex flex-col items-start" @click="openDownloadPicker()">
<span class="">Change download folder</span>
Expand Down Expand Up @@ -405,6 +411,7 @@ export default {
autostart: ref<boolean>(true),
realclose: ref<boolean>(false),
startminimized: ref<boolean>(false),
visibility: ref<Visibility>('Visible'),
downloadPath: ref<string | undefined>(),
Expand All @@ -430,6 +437,7 @@ export default {
}
await this.getRealclose(this);
await this.getStartMinimized(this);
await this.getDownloadPath(this);
// Check permission for notification
Expand Down
14 changes: 14 additions & 0 deletions app/main/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::{Arc, Mutex};

use rqs_lib::channel::{ChannelDirection, ChannelMessage};
use rqs_lib::{EndpointInfo, SendInfo, State, Visibility, RQS};
use store::get_startminimized;
use tauri::{
image::Image,
menu::{MenuBuilder, MenuItemBuilder},
Expand Down Expand Up @@ -138,6 +139,19 @@ async fn main() -> Result<(), anyhow::Error> {
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app_handle, event| match event {
tauri::RunEvent::Ready { .. } => {
trace!("RunEvent::Ready");
if get_startminimized(app_handle) {
#[cfg(not(target_os = "macos"))]
app_handle
.get_webview_window("main")
.unwrap()
.hide()
.unwrap();
#[cfg(target_os = "macos")]
app_handle.hide().unwrap();
}
}
tauri::RunEvent::ExitRequested { code, .. } => {
trace!("RunEvent::ExitRequested");
if code != Some(-1) {
Expand Down
20 changes: 20 additions & 0 deletions app/main/src-tauri/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn init_default(app_handle: &AppHandle) {
)
.ok();
}

if !store.has("startminimized") {
store
.insert("startminimized".to_owned(), JsonValue::Bool(false))
.ok();
}
Ok(())
},
);
Expand Down Expand Up @@ -113,3 +119,17 @@ pub fn get_logging_level(app_handle: &AppHandle) -> Option<String> {
.ok()
.flatten()
}

pub fn get_startminimized(app_handle: &AppHandle) -> bool {
with_store(
app_handle.clone(),
app_handle.state(),
".settings.json",
|store| Ok(store.get("startminimized").and_then(|json| json.as_bool())),
)
.unwrap_or_else(|e| {
error!("get_startminimized: error: {}", e);
None
})
.unwrap_or(false)
}
8 changes: 8 additions & 0 deletions app/main/src/components/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<input type="checkbox" :checked="!realclose" class="checkbox focus:outline-none">
</label>
</div>
<div class="form-control hover:bg-gray-500 hover:bg-opacity-10 rounded-xl p-3">
<label class="cursor-pointer flex flex-row justify-between items-center" @click="setStartMinimized(vm, !startminimized)">
<span class="label-text">Start minimized</span>
<input type="checkbox" :checked="startminimized" class="checkbox focus:outline-none">
</label>
</div>
<div class="form-control hover:bg-gray-500 hover:bg-opacity-10 rounded-xl p-3">
<label class="cursor-pointer flex flex-col items-start" @click="openDownloadPicker()">
<span class="">Change download folder</span>
Expand Down Expand Up @@ -401,6 +407,7 @@ export default {
autostart: ref<boolean>(true),
realclose: ref<boolean>(false),
startminimized: ref<boolean>(false),
visibility: ref<Visibility>('Visible'),
downloadPath: ref<string | undefined>(),
Expand All @@ -426,6 +433,7 @@ export default {
}
await this.getRealclose(this);
await this.getStartMinimized(this);
await this.getDownloadPath(this);
// Check permission for notification
Expand Down

0 comments on commit e084c4b

Please sign in to comment.