From 74963bc2f686520504d99a9d5b82474c9963ac08 Mon Sep 17 00:00:00 2001 From: Andy Bao Date: Sun, 30 May 2021 23:55:55 -0400 Subject: [PATCH] Fix options not being applied on extension start-up. Also cleanup some messy code. --- src/cmd/initialize.rs | 15 ++++++++------- src/ipc.rs | 35 +++++++++-------------------------- src/main.rs | 9 +++------ src/native_resp.rs | 14 +++++++++++++- src/options.rs | 12 ++++++++++++ 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/cmd/initialize.rs b/src/cmd/initialize.rs index fce7a51..256bb17 100644 --- a/src/cmd/initialize.rs +++ b/src/cmd/initialize.rs @@ -1,9 +1,10 @@ use crate::state::AppState; use crate::profiles::{ProfilesIniState, write_profiles}; use crate::native_req::NativeMessageInitialize; -use crate::native_resp::{NativeResponse, NativeResponseData, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponseEvent, NativeResponseProfileListProfileEntry}; +use crate::native_resp::{NativeResponse, NativeResponseData, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponseEvent, NativeResponseProfileListProfileEntry, write_native_event}; use std::{thread, fs}; use crate::ipc::setup_ipc; +use crate::options::native_notify_updated_options; pub fn process_cmd_initialize(app_state: &mut AppState, profiles: &mut ProfilesIniState, @@ -69,14 +70,14 @@ fn finish_init(app_state: &mut AppState, profiles: &mut ProfilesIniState, profil } // Notify extension of new profile list - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::ProfileList { - current_profile_id: profile_id.to_owned(), - profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect() - }) + write_native_event(NativeResponseEvent::ProfileList { + current_profile_id: profile_id.to_owned(), + profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect() }); + // Notify extension of current options + native_notify_updated_options(app_state); + // Begin IPC { let profile_id = profile_id.to_owned(); diff --git a/src/ipc.rs b/src/ipc.rs index 7fb1af8..9c07ff5 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -4,9 +4,9 @@ use crate::state::AppState; use byteorder::{WriteBytesExt, NetworkEndian, ReadBytesExt}; use std::io::{Write, Read, BufReader, BufWriter}; use std::path::PathBuf; -use crate::native_resp::{write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, NativeResponseEvent, NativeResponseProfileListProfileEntry}; +use crate::native_resp::{write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, NativeResponseEvent, NativeResponseProfileListProfileEntry, write_native_event}; use crate::profiles::{read_profiles, ProfilesIniState}; -use crate::options::read_global_options; +use crate::options::{read_global_options, native_notify_updated_options}; use crate::storage::options_data_path; use cfg_if::cfg_if; use serde::{Serialize, Deserialize}; @@ -108,11 +108,8 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) { match cmd { IPCCommand::FocusWindow(options) => { // Focus window - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::FocusWindow { - url: options.url - }) + write_native_event(NativeResponseEvent::FocusWindow { + url: options.url }); } IPCCommand::UpdateProfileList => { @@ -122,12 +119,9 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) { .as_ref() .map(|it| it.clone()) { // Notify updated profile list - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::ProfileList { - current_profile_id: pid.to_owned(), - profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect() - }) + write_native_event(NativeResponseEvent::ProfileList { + current_profile_id: pid.to_owned(), + profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect() }); } }, @@ -137,21 +131,10 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) { }; } IPCCommand::CloseManager => { - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::CloseManager) - }); + write_native_event(NativeResponseEvent::CloseManager); } IPCCommand::UpdateOptions => { - let new_options = read_global_options( - &options_data_path(&app_state.config_dir)); - - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::OptionsUpdated { - options: new_options - }) - }) + native_notify_updated_options(app_state); } } } diff --git a/src/main.rs b/src/main.rs index 544dcd4..36cb76b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ use rand::Rng; use crate::config::{read_configuration}; use crate::profiles::read_profiles; use crate::state::AppState; -use crate::native_resp::{NativeResponseEvent, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse}; +use crate::native_resp::{NativeResponseEvent, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, write_native_event}; use crate::cmd::execute_cmd_for_message; use crate::native_req::{read_incoming_message, NativeMessage}; @@ -58,11 +58,8 @@ mod state { fn main() { // Notify extension of our version - write_native_response(NativeResponseWrapper { - id: NATIVE_RESP_ID_EVENT, - resp: NativeResponse::event(NativeResponseEvent::ConnectorInformation { - version: APP_VERSION.to_string() - }) + write_native_event(NativeResponseEvent::ConnectorInformation { + version: APP_VERSION.to_string() }); // Calculate storage dirs diff --git a/src/native_resp.rs b/src/native_resp.rs index 76b0d49..033e21f 100644 --- a/src/native_resp.rs +++ b/src/native_resp.rs @@ -33,6 +33,15 @@ pub struct NativeResponseWrapper { pub resp: NativeResponse } +impl NativeResponseWrapper { + fn event(event: NativeResponseEvent) -> NativeResponseWrapper { + NativeResponseWrapper { + id: NATIVE_RESP_ID_EVENT, + resp: NativeResponse::event(event) + } + } +} + impl NativeResponse { pub fn error(msg: &str) -> NativeResponse { NativeResponse::Error { @@ -61,7 +70,7 @@ impl NativeResponse { data } } - pub fn event(event: NativeResponseEvent) -> NativeResponse { + fn event(event: NativeResponseEvent) -> NativeResponse { NativeResponse::Event(event) } } @@ -127,3 +136,6 @@ pub fn write_native_response(resp: NativeResponseWrapper) { handle.flush(); } +pub fn write_native_event(resp: NativeResponseEvent) { + write_native_response(NativeResponseWrapper::event(resp)); +} diff --git a/src/options.rs b/src/options.rs index acfb7d8..e7fa193 100644 --- a/src/options.rs +++ b/src/options.rs @@ -3,6 +3,9 @@ use std::collections::HashMap; use serde_json::Value; use std::fs::OpenOptions; use std::io; +use crate::storage::options_data_path; +use crate::native_resp::{write_native_response, NativeResponseWrapper, write_native_event, NativeResponseEvent}; +use crate::state::AppState; // === GLOBAL OPTIONS === @@ -36,3 +39,12 @@ pub fn write_global_options(path: &PathBuf, new_options: &HashMap serde_json::to_writer(options_file, &new_options) .map_err(WriteGlobalOptionsError::WriteFileError) } + +pub fn native_notify_updated_options(app_state: &AppState) { + let new_options = read_global_options( + &options_data_path(&app_state.config_dir)); + + write_native_event(NativeResponseEvent::OptionsUpdated { + options: new_options + }); +} \ No newline at end of file