-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further cleanup, tracing improvements
- Loading branch information
1 parent
491832f
commit 97b33f0
Showing
36 changed files
with
605 additions
and
431 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use self::app_state::AppState; | ||
use crate::{common, AppThread, ThreadMsg}; | ||
use common::app::{BackendMsg, BackendResponse, FrontendMsg, FrontendRequest}; | ||
|
||
mod app_state; | ||
|
||
/// All data required for managing the application. | ||
/// | ||
/// Includes its state and the handle to the Tauri application. | ||
pub struct AppData { | ||
pub state: AppState, | ||
tauri_app: tauri::AppHandle, | ||
} | ||
|
||
impl AppData { | ||
/// Create new app data. | ||
pub fn new(tauri_app: tauri::AppHandle) -> Self { | ||
Self { | ||
state: Default::default(), | ||
tauri_app, | ||
} | ||
} | ||
|
||
/// Associated Tauri application handle. | ||
pub fn tauri_app(&self) -> &tauri::AppHandle { | ||
&self.tauri_app | ||
} | ||
|
||
/// Handles a message from the parent thread. | ||
/// Returns `true` if the app should quit. | ||
pub fn handle_msg(&mut self, msg: ThreadMsg) -> bool { | ||
match msg { | ||
// Handle frontend message. | ||
ThreadMsg::Frontend(msg) => self.handle_frontend_msg(msg), | ||
// Quit. | ||
ThreadMsg::Exit => return true, | ||
}; | ||
false | ||
} | ||
|
||
/// Handles messages from the frontend. | ||
fn handle_frontend_msg(&mut self, msg: FrontendMsg) { | ||
match msg { | ||
FrontendMsg::Request(request) => { | ||
self.handle_frontend_request(request) | ||
} | ||
} | ||
} | ||
|
||
/// Handles the request and sends a response to the frontend. | ||
fn handle_frontend_request(&mut self, request: FrontendRequest) { | ||
// Handle request and prepare response. | ||
let response = match request { | ||
FrontendRequest::BackendAppState => { | ||
self.backend_app_state_response() | ||
} | ||
FrontendRequest::Settings => self.backend_settings_response(), | ||
FrontendRequest::UseDefaultSettings => { | ||
self.use_default_settings_response() | ||
} | ||
}; | ||
|
||
// Send response. | ||
AppThread::send_message( | ||
&self.tauri_app, | ||
BackendMsg::Response(response), | ||
); | ||
} | ||
|
||
/// Reply with current state. | ||
fn backend_app_state_response(&mut self) -> BackendResponse { | ||
// Get state and convert it to `BackendAppState`. | ||
let app_state = (&self.state).into(); | ||
|
||
// Prepare response. | ||
BackendResponse::BackendAppState(app_state) | ||
} | ||
|
||
/// Reply with current settings. | ||
fn backend_settings_response(&mut self) -> BackendResponse { | ||
// Get settings. | ||
let settings_opt = match self.state { | ||
AppState::Idle { ref settings } => Some(settings.clone()), | ||
_ => None, | ||
}; | ||
|
||
// Prepare response. | ||
BackendResponse::Settings(settings_opt) | ||
} | ||
|
||
/// Set default settings and reply with a copy. | ||
pub fn use_default_settings_response(&mut self) -> BackendResponse { | ||
// Apply and return settings. | ||
match self.state { | ||
// Fail gracefully if in the middle of reading settings. | ||
AppState::ReadingSettings => { | ||
tracing::error!("Frontend attempted to set settings to default while backend was still reading config."); | ||
} | ||
// Change state to idle if awaiting config. | ||
AppState::AwaitConfig { .. } => { | ||
self.state = AppState::Idle { | ||
settings: Default::default(), | ||
}; | ||
} | ||
// Modify if idle. | ||
AppState::Idle { ref mut settings } => { | ||
*settings = Default::default(); | ||
} | ||
// Modify if editing a project. | ||
AppState::Edit { | ||
ref mut settings, .. | ||
} => { | ||
*settings = Default::default(); | ||
} | ||
} | ||
|
||
// Prepare response. | ||
BackendResponse::UseDefaultSettings(Default::default()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub use edit_state::EditState; | ||
pub mod edit_state; | ||
|
||
use crate::common; | ||
use common::app::{AwaitConfigReason, BackendAppState}; | ||
use common::Settings; | ||
|
||
#[derive(Default)] | ||
/// Backend application state. | ||
pub enum AppState { | ||
#[default] | ||
/// Backend is in the process of reading user config. | ||
ReadingSettings, | ||
/// Settings read has been attempted, but no valid configuration was found. | ||
AwaitConfig { reason: AwaitConfigReason }, | ||
/// Backend is awaiting commands from the frontend. | ||
Idle { settings: Settings }, | ||
/// Backend is ready to edit a project. | ||
Edit { | ||
inner: Box<EditState>, | ||
settings: Settings, | ||
}, | ||
} | ||
|
||
/// Initialize app state from an optional configuration. | ||
impl From<Option<Settings>> for AppState { | ||
fn from(settings_opt: Option<Settings>) -> Self { | ||
match settings_opt { | ||
Some(settings) => AppState::Idle { settings }, | ||
None => AppState::AwaitConfig { | ||
reason: AwaitConfigReason::NoConfig, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// Convert app state to a minimal, serializable version. | ||
impl From<&AppState> for BackendAppState { | ||
fn from(app: &AppState) -> Self { | ||
match app { | ||
AppState::ReadingSettings => BackendAppState::ReadingSettings, | ||
AppState::AwaitConfig { ref reason } => { | ||
BackendAppState::AwaitConfig { reason: *reason } | ||
} | ||
AppState::Idle { .. } => BackendAppState::Idle, | ||
AppState::Edit { .. } => BackendAppState::Editor, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
pub mod audio_engine; | ||
|
||
use self::audio_engine::AudioEngine; | ||
use crate::common; | ||
use common::{Project, Settings}; | ||
|
||
pub struct EditState { | ||
pub project: Project, | ||
pub audio_engine: AudioEngine, | ||
} | ||
|
||
impl EditState { | ||
/// # Errors | ||
/// Failure if encountered problems while creating the audio engine. | ||
pub fn create_project( | ||
settings: &Settings, | ||
name: String, | ||
) -> Result<Self, audio_engine::Error> { | ||
let audio_engine = AudioEngine::from_settings(&settings.audio_engine)?; | ||
let editor = Self { | ||
audio_engine, | ||
project: Project::new(name), | ||
}; | ||
Ok(editor) | ||
} | ||
|
||
pub fn play_stream(&mut self) -> Result<(), cpal::PlayStreamError> { | ||
self.audio_engine.play() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.