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