Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve code naming a bit #23

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,4 @@ The release is automatically triggered upon push to the `release` branch. In ord

## Roadmap, known issues and feature requests

Roadmap(w/o order):

- [x] Create config file if not present
- [x] Drop MAC address requirement; add some better way - replaced with device name
- [x] Auto save MAC address upon first connection to be reused later
- [x] Adding new desk positions
- [x] Deleting desk positions
- [x] Windows support
- [x] MacOS support
- [x] Nicer icon
- [x] Better input window decorator- no need imo
- [x] Better desk moving behavior(currently it moves weirdly, due to usage of external lib)
- [x] More information inside README + potential problems
- [ ] Better tests
- [x] Run on system startup
- [x] Display a setup screen instead of automatic connection for better UX
- [x] Allow for config reset from inside the app
- [ ] Add options(low/high perf mode, system startup toggle)
- [ ] ~Automatic update prompts(?)~ who would want their desk manager app to connect to the internet
- [x] Automatic deploys
- [ ] low/high perf mode

Known issues(checked means fixed):

- [x] Clicking on newly added element
- [ ] Manually stopping a moving desk deadlocks the app
- [x] Opening a new window while the other is already open
- [ ] Race condition when moving the desk right after opening the app
Roadmap and issues are tracked in the [issues](https://github.com/golota60/trayasen/issues) and [project tracker](https://github.com/users/golota60/projects/3) respectively.
3 changes: 3 additions & 0 deletions src-tauri/src/config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub fn get_menu_items_from_config(config: &ConfigData) -> Vec<MenuConfigItem> {
.collect::<Vec<MenuConfigItem>>()
}

/**
Utility function returning the tray menu instance, based on the provided config
*/
pub fn create_main_tray_menu(config: &ConfigData) -> SystemTrayMenu {
let add_position_item = CustomMenuItem::new(ADD_POSITION_ID.to_string(), "Add a new position");
let manage_positions_item =
Expand Down
41 changes: 32 additions & 9 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ async fn connect_to_desk_by_name(name: String) -> Result<(), ()> {

fn main() {
let config = config_utils::get_or_create_config();

let initiated_desk = TauriSharedDesk(None.into());

// Get the desk before running the app if possible, so that user doesn't see any loading screens
// instead app just starts up slower
/*
If there is a desk name present already, do not bother the end user with windows opening/loading. Just connect to his desk.
*/
let local_name = &config.local_name;
block_on(async {
if let Some(local_name) = local_name.clone() {
Expand All @@ -94,29 +94,39 @@ fn main() {

println!("Loaded config: {:?}", config);

let tray = config_utils::create_main_tray_menu(&config);
let tray = SystemTray::new().with_menu(tray);
let tray_skeleton = config_utils::create_main_tray_menu(&config);
let tray = SystemTray::new().with_menu(tray_skeleton);

tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
// Pass the tray instance to tauri to manage
.system_tray(tray)
// Pass the desk instance to tauri to manage
.manage(initiated_desk)
// Pass the previously instantiates config. We ideally want to read fs only once.
.manage(config)
.setup(|app| {
let config = config_utils::get_or_create_config();
/*
On setup, we only wanna bail early if we're already connected
and register all the shortcuts
*/
let config = app.state::<config_utils::ConfigData>();

let loc_name = &config.local_name;
let window = app.get_window("main").unwrap();

if let Some(_) = loc_name {
// If the user is returning(has a config) immidiately close the window, not to eat resources
// And then proceed to try to connect to the provided desk name.
window
.close()
.expect("Error while closing the initial window");
let mut shortcut_manager = app.global_shortcut_manager();
let all_positions = &config.saved_positions;
let desk_state = app.state::<TauriSharedDesk>();

// We expect the desk to already exist at this point, since if loc_name, the first thing we do in the app is connect
let desk = desk_state
.0
.lock()
Expand All @@ -125,9 +135,12 @@ fn main() {
.as_ref()
.expect("Desk should have been defined at this point");

// Register all shortcuts
let mut shortcut_manager = app.global_shortcut_manager();
let all_positions = &config.saved_positions;
let cloned_pos = all_positions.clone();
for pos in cloned_pos.into_iter() {
// Each iteration needs it's own clone
// Each iteration needs it's own clone; we do not want to consume the app state
let cloned_desk = desk.clone();
if let Some(shortcut_key) = &pos.shortcut {
if shortcut_key != "" {
Expand All @@ -149,6 +162,7 @@ fn main() {

Ok(())
})
// Pass functions invokable on frontend
.invoke_handler(tauri::generate_handler![
create_new_elem,
config_utils::get_config,
Expand All @@ -158,6 +172,7 @@ fn main() {
connect_to_desk_by_name,
])
.enable_macos_default_menu(false)
// Register all the tray events, eg. clicks and stuff
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
config_utils::QUIT_ID => tray_utils::handle_exit_menu_click(),
Expand Down Expand Up @@ -197,15 +212,23 @@ fn main() {
.expect("error while running tauri application")
.run(move |app_handle, event| match event {
tauri::RunEvent::Ready => {}
/*
Exit requested, might mean that a new position has been added.
This is troublesome; since all the positions are actually system tray elements, we need to re-instantiate it
So, when we detected an exit requested, just to be safe, refresh the system tray.
TODO: We should probably have a way of checking for new elements, to remove redundant system tray refreshes
*/
tauri::RunEvent::ExitRequested { api, .. } => {
// Exit requested might mean that a new element has been added.
println!("Exit requested");
let config = config_utils::get_config();
let main_menu = config_utils::create_main_tray_menu(&config);
app_handle
.tray_handle()
.set_menu(main_menu)
.expect("Error whilst unwrapping main menu");

// Do not actually exit the app
api.prevent_exit();
}
_ => {}
Expand Down