Skip to content

Commit 083ad87

Browse files
authored
Rebuild on ticks rather than each update (#51)
* Rebuild on ticks rather than each update * Remove logged line
1 parent 44a8c62 commit 083ad87

5 files changed

Lines changed: 86 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rubato = "0.16"
1818
serde = { version = "1.0", features = ["derive"] }
1919
serde_json = "1.0"
2020
crossbeam = "0.8"
21-
iced = "0.13"
21+
iced = { version = "0.13", features = ["tokio"] }
2222
log = "0.4"
2323
env_logger = "0.11"
2424
dotenv = "0.15"

src/gui/app.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use iced::{Element, Length, Task, Theme};
1+
use iced::{Element, Length, Subscription, Task, Theme, time, time::Duration};
22
use log::{error, info};
33

44
use crate::gui::components::{
@@ -24,6 +24,8 @@ pub struct AmplifierApp {
2424
show_save_input: bool,
2525
settings: Settings,
2626
settings_dialog: SettingsDialog,
27+
28+
dirty_chain: bool,
2729
}
2830

2931
impl AmplifierApp {
@@ -53,7 +55,7 @@ impl AmplifierApp {
5355
let control_bar = Control::new(StageType::default());
5456
let settings_dialog = SettingsDialog::new(&settings.audio);
5557

56-
let app = Self {
58+
Self {
5759
processor_manager,
5860
stages,
5961
is_recording: false,
@@ -66,43 +68,54 @@ impl AmplifierApp {
6668
show_save_input: false,
6769
settings,
6870
settings_dialog,
69-
};
7071

71-
// Update the processor chain with the loaded preset
72-
app.update_processor_chain();
72+
// Set dirty chain to true to trigger initial rebuild
73+
dirty_chain: true,
74+
}
75+
}
7376

74-
app
77+
pub fn subscription(&self) -> Subscription<Message> {
78+
if self.dirty_chain {
79+
time::every(Duration::from_millis(100)).map(|_| Message::RebuildTick)
80+
} else {
81+
Subscription::none()
82+
}
7583
}
7684

7785
pub fn update(&mut self, message: Message) -> Task<Message> {
78-
let mut should_update_chain = false;
7986
let mut should_update_stages = false;
8087

8188
match message {
89+
Message::RebuildTick => {
90+
if self.dirty_chain {
91+
self.update_processor_chain();
92+
self.dirty_chain = false;
93+
}
94+
}
8295
Message::AddStage => {
8396
let new_stage = StageConfig::create_default(self.control_bar.selected());
8497
self.stages.push(new_stage);
85-
should_update_chain = true;
98+
self.dirty_chain = true;
8699
should_update_stages = true;
87100
}
88101
Message::RemoveStage(idx) => {
89102
if idx < self.stages.len() {
90103
self.stages.remove(idx);
91104
}
92-
should_update_chain = true;
105+
self.dirty_chain = true;
93106
should_update_stages = true;
94107
}
95108
Message::MoveStageUp(idx) => {
96109
if idx > 0 {
97110
self.stages.swap(idx - 1, idx);
98-
should_update_chain = true;
111+
self.dirty_chain = true;
99112
should_update_stages = true;
100113
}
101114
}
102115
Message::MoveStageDown(idx) => {
103116
if idx < self.stages.len().saturating_sub(1) {
104117
self.stages.swap(idx, idx + 1);
105-
should_update_chain = true;
118+
self.dirty_chain = true;
106119
should_update_stages = true;
107120
}
108121
}
@@ -115,7 +128,7 @@ impl AmplifierApp {
115128
self.selected_preset = Some(preset_name.clone());
116129
self.preset_bar
117130
.set_selected_preset(Some(preset_name.clone()));
118-
should_update_chain = true;
131+
self.dirty_chain = true;
119132
should_update_stages = true;
120133
info!("Loaded preset: {preset_name}");
121134
}
@@ -192,7 +205,7 @@ impl AmplifierApp {
192205
self.stages.clear();
193206
}
194207

195-
should_update_chain = true;
208+
self.dirty_chain = true;
196209
}
197210
}
198211
Err(e) => {
@@ -286,16 +299,12 @@ impl AmplifierApp {
286299
}
287300
Message::Stage(idx, stage_msg) => {
288301
if self.update_stage(idx, stage_msg) {
289-
should_update_chain = true;
302+
self.dirty_chain = true;
290303
should_update_stages = true
291304
}
292305
}
293306
}
294307

295-
if should_update_chain {
296-
self.update_processor_chain();
297-
}
298-
299308
if should_update_stages {
300309
self.stage_list.set_stages(&self.stages);
301310
}

src/gui/messages.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum Message {
1111
MoveStageUp(usize),
1212
MoveStageDown(usize),
1313
StageTypeSelected(StageType),
14+
RebuildTick,
1415

1516
// Preset settings
1617
PresetSelected(String),

src/gui/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const DEFAULT_FONT: Font = Font::MONOSPACE;
1515

1616
pub fn start(processor_manager: ProcessorManager, settings: Settings) -> iced::Result {
1717
iced::application("Rustortion", AmplifierApp::update, AmplifierApp::view)
18+
.subscription(AmplifierApp::subscription)
1819
.window_size((800.0, 600.0))
1920
.theme(AmplifierApp::theme)
2021
.default_font(DEFAULT_FONT)

0 commit comments

Comments
 (0)