Skip to content

Commit

Permalink
#1391 Inform app when instances change
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Jan 7, 2025
1 parent a2db407 commit ebffc93
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 58 deletions.
20 changes: 11 additions & 9 deletions main/src/application/unit_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ pub trait SessionUi {
fn handle_internal_info_event(&self, event: &InternalInfoEvent);
fn handle_external_info_event(&self, event: InstanceInfoEvent);
fn handle_everything_changed(&self, unit_model: &UnitModel);
fn handle_unit_name_changed(&self);
fn handle_global_control_and_feedback_state_changed(&self);
fn handle_affected(
&self,
Expand All @@ -88,7 +87,7 @@ pub struct UnitModel {
/// Persisted and can be user-customized. Should be
/// unique but if not it's not a big deal, then it won't crash but the user can't be sure which
/// session will be picked. Most relevant for HTTP/WS API.
pub unit_key: Prop<String>,
unit_key: String,
pub name: Option<String>,
pub let_matched_events_through: Prop<bool>,
pub let_unmatched_events_through: Prop<bool>,
Expand Down Expand Up @@ -255,7 +254,7 @@ impl UnitModel {
.map(|au| au.controller_id.clone())
.unwrap_or_else(|| nanoid::nanoid!(8));
let mut model = Self {
unit_key: prop(initial_unit_key),
unit_key: initial_unit_key,
instance_id,
unit_id,
let_matched_events_through: prop(session_defaults::LET_MATCHED_EVENTS_THROUGH),
Expand Down Expand Up @@ -344,7 +343,7 @@ impl UnitModel {
}

pub fn name_or_key(&self) -> &str {
self.name.as_deref().unwrap_or(self.unit_key.get_ref())
self.name.as_deref().unwrap_or(&self.unit_key)
}

pub fn auto_unit(&self) -> Option<&AutoUnitData> {
Expand Down Expand Up @@ -402,7 +401,7 @@ impl UnitModel {
}

pub fn unit_key(&self) -> &str {
self.unit_key.get_ref()
&self.unit_key
}

pub fn instance_track_descriptor(&self) -> &TrackDescriptor {
Expand Down Expand Up @@ -1109,6 +1108,10 @@ impl UnitModel {
self.name = unit_name;
Some(One(SessionProp::UnitName))
}
C::SetUnitKey(unit_key) => {
self.unit_key = unit_key;
Some(One(SessionProp::UnitKey))
}
C::SetInstanceTrack(api_desc) => {
let virtual_track =
domain::TrackDescriptor::from_api(api_desc.clone()).unwrap_or_default();
Expand Down Expand Up @@ -1277,9 +1280,6 @@ impl UnitModel {
use SessionProp::*;
let mut session = session.borrow_mut();
match &affected {
One(UnitName) => {
session.ui().handle_unit_name_changed();
}
One(WantsKeyboardInput | StreamDeckDeviceId) => {
session.sync_settings();
}
Expand Down Expand Up @@ -2444,7 +2444,7 @@ impl UnitModel {
- Controller mapping subscription count: {}\n\
",
self.unit_id,
self.unit_key.get_ref(),
&self.unit_key,
self.mappings[CompartmentKind::Main].len(),
self.mapping_subscriptions[CompartmentKind::Main].len(),
self.groups.len(),
Expand Down Expand Up @@ -3029,6 +3029,7 @@ pub fn reaper_supports_global_midi_filter() -> bool {
#[allow(dead_code)]
pub enum SessionCommand {
SetUnitName(Option<String>),
SetUnitKey(String),
SetInstanceTrack(TrackDescriptor),
SetInstanceFx(FxDescriptor),
SetWantsKeyboardInput(bool),
Expand All @@ -3039,6 +3040,7 @@ pub enum SessionCommand {

pub enum SessionProp {
UnitName,
UnitKey,
InstanceTrack,
InstanceFx,
WantsKeyboardInput,
Expand Down
6 changes: 3 additions & 3 deletions main/src/infrastructure/data/unit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,6 @@ impl UnitData {
)?;
// Mutation
let migration_descriptor = MigrationDescriptor::new(self.version.as_ref());
if let Some(id) = &self.id {
session.unit_key.set_without_notification(id.clone())
};
session.lives_on_upper_floor.set(self.lives_on_upper_floor);
session
.send_feedback_only_if_armed
Expand Down Expand Up @@ -848,6 +845,9 @@ impl UnitData {
session.tags.set_without_notification(self.tags.clone());
session.set_instance_preset_link_config(self.instance_preset_link_config.clone());
session.set_use_unit_preset_links_only(self.use_instance_preset_links_only);
if let Some(id) = &self.id {
let _ = session.change(SessionCommand::SetUnitKey(id.clone()));
};
let _ = session.change(SessionCommand::SetUnitName(self.name.clone()));
let _ = session.change(SessionCommand::SetInstanceTrack(
self.instance_track.clone(),
Expand Down
2 changes: 2 additions & 0 deletions main/src/infrastructure/plugin/backbone_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ impl BackboneShell {
self.control_surface_main_task_sender.0.send_complaining(
RealearnControlSurfaceMainTask::AddInstance(instance_id, instance),
);
self.proto_hub.notify_instances_changed();
}

pub fn unregister_instance(&self, instance_id: InstanceId) {
Expand All @@ -1474,6 +1475,7 @@ impl BackboneShell {
});
self.audio_hook_task_sender
.send_complaining(NormalAudioHookTask::RemoveRealTimeInstance(instance_id));
self.proto_hub.notify_instances_changed();
}

pub fn register_unit(
Expand Down
2 changes: 1 addition & 1 deletion main/src/infrastructure/proto/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl occasional_instance_update::Update {
let unit_model = unit_model.borrow();
Unit {
id: unit_model.unit_id().into(),
key: unit_model.unit_key.get_ref().clone(),
key: unit_model.unit_key().to_string(),
name: unit_model.name().map(|n| n.to_string()),
}
});
Expand Down
4 changes: 4 additions & 0 deletions main/src/infrastructure/proto/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl ProtoHub {
});
}

pub fn notify_instances_changed(&self) {
self.send_occasional_global_updates(|| [occasional_global_update::Update::instances()]);
}

pub fn notify_midi_input_devices_changed(&self) {
self.send_occasional_global_updates(|| {
[occasional_global_update::Update::midi_input_devices()]
Expand Down
20 changes: 8 additions & 12 deletions main/src/infrastructure/server/http/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,12 @@ pub fn keep_informing_clients_about_session_events(shared_session: &SharedUnitMo
.do_async(|session, _| {
let _ = send_updated_active_controller(&session.borrow());
});
when(
session
.everything_changed()
.merge(session.unit_key.changed()),
)
.with(Rc::downgrade(shared_session))
.do_async(|session, _| {
send_sessions_to_subscribed_clients();
let session = session.borrow();
let _ = send_updated_active_controller(&session);
let _ = send_updated_controller_routing(&session);
});
when(session.everything_changed())
.with(Rc::downgrade(shared_session))
.do_async(|session, _| {
send_sessions_to_subscribed_clients();
let session = session.borrow();
let _ = send_updated_active_controller(&session);
let _ = send_updated_controller_routing(&session);
});
}
16 changes: 14 additions & 2 deletions main/src/infrastructure/ui/companion_app_presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(clippy::useless_let_if_seq)]
use askama::Template;

use crate::application::{SharedUnitModel, WeakUnitModel};
use crate::application::{Affected, SessionProp, SharedUnitModel, WeakUnitModel};
use crate::base::when;
use crate::infrastructure::plugin::BackboneShell;

Expand Down Expand Up @@ -37,6 +37,19 @@ impl CompanionAppPresenter {
shared
}

pub fn handle_affected(&self, affected: &Affected<SessionProp>, _initiator: Option<u32>) {
use crate::application::Affected::One;
use crate::application::SessionProp::UnitKey;
match affected {
One(UnitKey) => {
if self.app_info_requested.get() {
self.update_app_info();
}
}
_ => {}
}
}

pub fn show_app_info(&self) {
self.app_info_requested.set(true);
let index_file = self.update_app_info();
Expand Down Expand Up @@ -114,7 +127,6 @@ impl CompanionAppPresenter {
when(
app.changed()
.merge(app.server().borrow().changed())
.merge(self.session().borrow().unit_key.changed())
.take_until(self.party_is_over()),
)
.with(Rc::downgrade(self))
Expand Down
2 changes: 2 additions & 0 deletions main/src/infrastructure/ui/header_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ impl HeaderPanel {
}

pub fn handle_affected(&self, affected: &Affected<SessionProp>, initiator: Option<u32>) {
self.companion_app_presenter
.handle_affected(affected, initiator);
if !self.is_open() {
return;
}
Expand Down
64 changes: 33 additions & 31 deletions main/src/infrastructure/ui/unit_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use reaper_high::Reaper;
use std::cell::RefCell;
use std::fmt;

use crate::application::SessionProp::UnitName;
use crate::application::{
get_virtual_fx_label, get_virtual_track_label, Affected, CompartmentProp, SessionCommand,
SessionProp, SessionUi, UnitModel, VirtualFxType, WeakUnitModel,
Expand All @@ -21,7 +22,8 @@ use crate::domain::{
};
use crate::infrastructure::plugin::{update_auto_units_async, BackboneShell};
use crate::infrastructure::server::http::{
send_projection_feedback_to_subscribed_clients, send_updated_controller_routing,
send_projection_feedback_to_subscribed_clients, send_sessions_to_subscribed_clients,
send_updated_controller_routing,
};
use crate::infrastructure::ui::instance_panel::InstancePanel;
use crate::infrastructure::ui::util::{header_panel_height, parse_tags_from_csv};
Expand All @@ -41,7 +43,7 @@ type _MainPanel = UnitPanel;
pub struct UnitPanel {
instance_id: InstanceId,
view: ViewContext,
session: WeakUnitModel,
unit_model: WeakUnitModel,
instance_panel: WeakView<InstancePanel>,
header_panel: SharedView<HeaderPanel>,
mapping_rows_panel: SharedView<MappingRowsPanel>,
Expand All @@ -63,7 +65,7 @@ impl UnitPanel {
instance_id,
view: Default::default(),
state: state.clone(),
session: session.clone(),
unit_model: session.clone(),
instance_panel: instance_panel.clone(),
header_panel: HeaderPanel::new(
session.clone(),
Expand Down Expand Up @@ -197,7 +199,7 @@ impl UnitPanel {
}

fn do_with_session<R>(&self, f: impl FnOnce(&UnitModel) -> R) -> Result<R, &'static str> {
match self.session.upgrade() {
match self.unit_model.upgrade() {
None => Err("session not available anymore"),
Some(session) => Ok(f(&session.borrow())),
}
Expand All @@ -207,7 +209,7 @@ impl UnitPanel {
&self,
f: impl FnOnce(&mut UnitModel) -> R,
) -> Result<R, &'static str> {
match self.session.upgrade() {
match self.unit_model.upgrade() {
None => Err("session not available anymore"),
Some(session) => Ok(f(&mut session.borrow_mut())),
}
Expand Down Expand Up @@ -263,12 +265,6 @@ impl UnitPanel {
self.when(session.everything_changed(), |view| {
view.invalidate_all_controls();
});
self.when(
session.tags.changed().merge(session.unit_key.changed()),
|view| {
view.invalidate_status_1_text();
},
);
let instance_state = session.unit().borrow();
self.when(
instance_state.global_control_and_feedback_state_changed(),
Expand Down Expand Up @@ -339,24 +335,27 @@ impl UnitPanel {
.notify_about_instance_info_event(self.instance_id, event);
}

fn handle_unit_name_changed(&self) {
if let Ok(shell) = self.instance_panel().shell() {
// At the time when this method is called, the unit is still borrowed, so the proto hub can't create
// an overview over all units.
// TODO-medium It would be better if we would send around tiny events here and collect
// them from the event loop.
let _ =
Global::task_support().do_later_in_main_thread_from_main_thread_asap(move || {
fn handle_affected_own(self: SharedView<Self>, affected: Affected<SessionProp>) {
use Affected::*;
use SessionProp::*;
// Handle even if closed
match affected {
One(UnitName) => {
if let Ok(shell) = self.instance_panel().shell() {
BackboneShell::get()
.proto_hub()
.notify_instance_units_changed(&shell);
});
}
}
One(UnitKey) => {
// Actually, the instances are only affected if the main unit key is changed, but
// so what.
BackboneShell::get().proto_hub().notify_instances_changed();
send_sessions_to_subscribed_clients();
}
_ => {}
}
}

fn handle_affected_own(self: SharedView<Self>, affected: Affected<SessionProp>) {
use Affected::*;
use SessionProp::*;
// Handle only if open
if !self.is_open() {
return;
}
Expand All @@ -367,6 +366,9 @@ impl UnitPanel {
One(UnitName) => {
let _ = self.invalidate_unit_button();
}
One(UnitKey) => {
self.invalidate_status_1_text();
}
Multiple => {
self.invalidate_all_controls();
}
Expand Down Expand Up @@ -453,7 +455,7 @@ impl UnitPanel {
session.change_with_notification(
SessionCommand::SetUnitName(unit_name),
None,
self.session.clone(),
self.unit_model.clone(),
);
})?;
// Take care of unit key
Expand All @@ -468,7 +470,11 @@ impl UnitPanel {
return Ok(());
}
self.do_with_session_mut(|session| {
session.unit_key.set(new_unit_key);
session.change_with_notification(
SessionCommand::SetUnitKey(new_unit_key),
None,
self.unit_model.clone(),
);
})?;
}
Ok(())
Expand Down Expand Up @@ -596,10 +602,6 @@ impl SessionUi for Weak<UnitPanel> {
.notify_everything_in_unit_has_changed(unit_model.instance_id(), unit_model.unit_id());
}

fn handle_unit_name_changed(&self) {
upgrade_panel(self).handle_unit_name_changed();
}

fn handle_global_control_and_feedback_state_changed(&self) {
update_auto_units_async();
}
Expand Down

0 comments on commit ebffc93

Please sign in to comment.