Skip to main content

Mountain/Environment/OutputProvider/
ChannelVisibility.rs

1//! # Output Channel Visibility Helpers
2//!
3//! Internal helper functions for output channel UI visibility operations.
4//! These are not public API - they are called by the main provider
5//! implementation.
6
7use CommonLibrary::{Error::CommonError::CommonError, IPC::SkyEvent::SkyEvent};
8use serde_json::json;
9use tauri::Emitter;
10
11use crate::{Environment::Utility, dev_log};
12
13/// Reveals an output channel in the UI.
14pub(super) async fn reveal_channel(
15	env:&crate::Environment::MountainEnvironment::MountainEnvironment,
16
17	channel_identifier:String,
18
19	preserve_focus:bool,
20) -> Result<(), CommonError> {
21	dev_log!("output", "[OutputProvider] Revealing channel: '{}'", channel_identifier);
22
23	let mut channels_guard = env
24		.ApplicationState
25		.Feature
26		.OutputChannels
27		.OutputChannels
28		.lock()
29		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
30
31	if let Some(channel_state) = channels_guard.get_mut(&channel_identifier) {
32		channel_state.IsVisible = true;
33
34		let event_payload = json!({ "channel": channel_identifier, "preserveFocus": preserve_focus });
35
36		env.ApplicationHandle
37			.emit(SkyEvent::OutputReveal.AsStr(), event_payload)
38			.map_err(|Error| CommonError::UserInterfaceInteraction { Reason:Error.to_string() })?;
39	} else {
40		dev_log!(
41			"output",
42			"warn: [OutputProvider] Channel '{}' not found for reveal.",
43			channel_identifier
44		);
45	}
46
47	Ok(())
48}
49
50/// Closes the view of an output channel in the UI. Hides the channel
51/// (mutates `IsVisible` in `ApplicationState`) and emits a Sky event
52/// so the renderer can collapse the panel; the channel itself stays
53/// in state with its buffered lines so a later `reveal` can re-open
54/// it without losing content. To remove the channel entirely, use
55/// `dispose_channel` instead.
56pub(super) async fn close_channel(
57	env:&crate::Environment::MountainEnvironment::MountainEnvironment,
58
59	channel_identifier:String,
60) -> Result<(), CommonError> {
61	dev_log!("output", "[OutputProvider] Closing channel: '{}'", channel_identifier);
62
63	let mut channels_guard = env
64		.ApplicationState
65		.Feature
66		.OutputChannels
67		.OutputChannels
68		.lock()
69		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
70
71	if let Some(channel_state) = channels_guard.get_mut(&channel_identifier) {
72		channel_state.IsVisible = false;
73
74		// Re-use OutputReveal with `PreserveFocus: false` to push the
75		// updated visibility state - SkyEvent doesn't yet have a
76		// dedicated Hide variant; the renderer's reveal handler is
77		// idempotent and reads the latest IsVisible from state.
78		let event_payload = json!({ "channel": channel_identifier, "preserveFocus": true, "visible": false });
79
80		env.ApplicationHandle
81			.emit(SkyEvent::OutputReveal.AsStr(), event_payload)
82			.map_err(|Error| CommonError::UserInterfaceInteraction { Reason:Error.to_string() })?;
83	} else {
84		dev_log!(
85			"output",
86			"warn: [OutputProvider] Channel '{}' not found for close.",
87			channel_identifier
88		);
89	}
90
91	Ok(())
92}