Skip to content

Commit

Permalink
Propagate live config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
William Calliari committed Oct 10, 2024
1 parent b85818c commit 9a66d8c
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 33 deletions.
7 changes: 4 additions & 3 deletions cosmic-comp-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ fn default_repeat_delay() -> u32 {
600
}


#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StackBehavior {
pub close_tab_on_middle_click: bool,
}

impl Default for StackBehavior {
fn default() -> Self {
Self { close_tab_on_middle_click: true }
Self {
close_tab_on_middle_click: true,
}
}
}
}
12 changes: 9 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ mod types;
pub use self::types::*;
use cosmic::config::CosmicTk;
use cosmic_comp_config::{
input::InputConfig, workspace::WorkspaceConfig, CosmicCompConfig, StackBehavior, TileBehavior, XkbConfig
input::InputConfig, workspace::WorkspaceConfig, CosmicCompConfig, StackBehavior, TileBehavior,
XkbConfig,
};

#[derive(Debug)]
Expand Down Expand Up @@ -693,8 +694,13 @@ fn config_changed(config: cosmic_config::Config, keys: Vec<String>, state: &mut
"stack_behavior" => {
let new = get_config::<StackBehavior>(&config, "stack_behavior");
if new != state.common.config.cosmic_conf.stack_behavior {
state.common.config.cosmic_conf.stack_behavior = new;
state.common.update_config();
state.common.config.cosmic_conf.stack_behavior = new.clone();

let mut shell = state.common.shell.write().unwrap();
let shell_ref = &mut *shell;
shell_ref
.workspaces
.update_stack_behavior(new, shell_ref.seats.iter())
}
}
"active_hint" => {
Expand Down
7 changes: 7 additions & 0 deletions src/shell/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,13 @@ impl CosmicMapped {
_ => unreachable!(),
})
}

pub fn update_stack_behavior(&mut self, behavior: &StackBehavior) {
if let CosmicMappedInternal::Stack(stack) = &mut self.element {
let mut inner = stack.0 .0.lock().unwrap();
inner.program_mut().update_behavior(behavior.clone());
}
}
}

impl IsAlive for CosmicMapped {
Expand Down
4 changes: 4 additions & 0 deletions src/shell/element/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl CosmicStackInternal {
pub fn current_focus(&self) -> Option<Focus> {
unsafe { Focus::from_u8(self.pointer_entered.load(Ordering::SeqCst)) }
}

pub fn update_behavior(&mut self, behavior: StackBehavior) {
self.behavior = behavior;
}
}

pub const TAB_HEIGHT: i32 = 24;
Expand Down
3 changes: 1 addition & 2 deletions src/shell/element/stack/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,12 @@ where
if matches!(
event,
event::Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Middle))
) {
) {
if let Some(message) = self.middle_click_message.clone() {
shell.publish(message);
return event::Status::Captured;
}
}

}

status
Expand Down
6 changes: 5 additions & 1 deletion src/shell/layout/floating/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,11 @@ impl FloatingLayout {
if mapped.is_window() {
// if it is just a window
self.space.unmap_elem(&mapped);
mapped.convert_to_stack((&output, mapped.bbox()), self.theme.clone(), stack_behavior.clone());
mapped.convert_to_stack(
(&output, mapped.bbox()),
self.theme.clone(),
stack_behavior.clone(),
);
self.map_internal(
mapped.clone(),
Some(location.as_local()),
Expand Down
29 changes: 23 additions & 6 deletions src/shell/layout/tiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct TilingLayout {
swapping_stack_surface_id: Id,
last_overview_hover: Option<(Option<Instant>, TargetZone)>,
pub theme: cosmic::Theme,
stack_behavior: StackBehavior,
pub stack_behavior: StackBehavior,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -344,7 +344,11 @@ pub struct MinimizedTilingState {
}

impl TilingLayout {
pub fn new(theme: cosmic::Theme, output: &Output, stack_behavior: StackBehavior) -> TilingLayout {
pub fn new(
theme: cosmic::Theme,
output: &Output,
stack_behavior: StackBehavior,
) -> TilingLayout {
TilingLayout {
queue: TreeQueue {
trees: {
Expand Down Expand Up @@ -2124,7 +2128,7 @@ impl TilingLayout {
&mut self,
mapped: &CosmicMapped,
mut focus_stack: FocusStackMut,
stack_behavior: &StackBehavior
stack_behavior: &StackBehavior,
) -> Option<KeyboardFocusTarget> {
let gaps = self.gaps();

Expand All @@ -2141,7 +2145,11 @@ impl TilingLayout {
// if it is just a window
match tree.get_mut(&node_id).unwrap().data_mut() {
Data::Mapped { mapped, .. } => {
mapped.convert_to_stack((&self.output, mapped.bbox()), self.theme.clone(), stack_behavior.clone());
mapped.convert_to_stack(
(&self.output, mapped.bbox()),
self.theme.clone(),
stack_behavior.clone(),
);
focus_stack.append(&mapped);
KeyboardFocusTarget::Element(mapped.clone())
}
Expand Down Expand Up @@ -2279,7 +2287,12 @@ impl TilingLayout {
return None;
}
let handle = handle.unwrap();
let stack = CosmicStack::new(surfaces.into_iter(), handle, self.theme.clone(), stack_behavior.clone());
let stack = CosmicStack::new(
surfaces.into_iter(),
handle,
self.theme.clone(),
stack_behavior.clone(),
);

for child in tree
.children_ids(&last_active)
Expand Down Expand Up @@ -2746,7 +2759,11 @@ impl TilingLayout {
Some(TargetZone::WindowStack(window_id, _)) if tree.get(&window_id).is_ok() => {
match tree.get_mut(window_id).unwrap().data_mut() {
Data::Mapped { mapped, .. } => {
mapped.convert_to_stack((&self.output, mapped.bbox()), self.theme.clone(), self.stack_behavior.clone());
mapped.convert_to_stack(
(&self.output, mapped.bbox()),
self.theme.clone(),
self.stack_behavior.clone(),
);
let Some(stack) = mapped.stack_ref_mut() else {
unreachable!()
};
Expand Down
82 changes: 64 additions & 18 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use wayland_backend::server::ClientId;

use crate::wayland::{handlers::data_device, protocols::workspace::WorkspaceCapabilities};
use cosmic_comp_config::{
workspace::{WorkspaceLayout, WorkspaceMode}, StackBehavior, TileBehavior
workspace::{WorkspaceLayout, WorkspaceMode},
StackBehavior, TileBehavior,
};
use cosmic_protocols::workspace::v1::server::zcosmic_workspace_handle_v1::{
State as WState, TilingState,
Expand Down Expand Up @@ -347,7 +348,13 @@ fn create_workspace(
&workspace_handle,
[WorkspaceCapabilities::Activate].into_iter(),
);
Workspace::new(workspace_handle, output.clone(), tiling, theme.clone(), stack_behavior)
Workspace::new(
workspace_handle,
output.clone(),
tiling,
theme.clone(),
stack_behavior,
)
}

fn move_workspace_to_group(
Expand Down Expand Up @@ -429,7 +436,7 @@ impl WorkspaceSet {
true,
tiling_enabled,
theme.clone(),
stack_behavior.clone()
stack_behavior.clone(),
);
workspace_set_idx(state, 1, idx, &workspace.handle);
state.set_workspace_capabilities(
Expand Down Expand Up @@ -858,7 +865,7 @@ impl Workspaces {
false,
config.cosmic_conf.autotile,
self.theme.clone(),
self.stack_behavior.clone()
self.stack_behavior.clone(),
),
);
}
Expand Down Expand Up @@ -1127,6 +1134,34 @@ impl Workspaces {
self.autotile = autotile;
self.apply_tile_change(guard, seats);
}

pub fn update_stack_behavior<'a>(
&mut self,
behavior: StackBehavior,
seats: impl Iterator<Item = &'a Seat<State>>,
) {
let seats = seats.cloned().collect::<Vec<_>>();
self.stack_behavior = behavior.clone();
for (_, set) in &mut self.sets {
set.stack_behavior = behavior.clone();

for w in &mut set.workspaces {
w.tiling_layer.stack_behavior = behavior.clone();
for seat in &seats {
let stack = w.focus_stack.get_mut(seat);
*stack.0 = stack
.0
.clone()
.into_iter()
.map(|mut window| {
window.update_stack_behavior(&behavior);
window
})
.collect();
}
}
}
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -3580,13 +3615,17 @@ impl Shell {
.toggle_stacking(window, workspace.focus_stack.get_mut(seat), &behavior)
} else if let Some(workspace) = self.space_for_mut(window) {
if workspace.tiling_layer.mapped().any(|(m, _)| m == window) {
workspace
.tiling_layer
.toggle_stacking(window, workspace.focus_stack.get_mut(seat), &behavior)
workspace.tiling_layer.toggle_stacking(
window,
workspace.focus_stack.get_mut(seat),
&behavior,
)
} else if workspace.floating_layer.mapped().any(|w| w == window) {
workspace
.floating_layer
.toggle_stacking(window, workspace.focus_stack.get_mut(seat), &behavior)
workspace.floating_layer.toggle_stacking(
window,
workspace.focus_stack.get_mut(seat),
&behavior,
)
} else {
None
}
Expand All @@ -3605,16 +3644,23 @@ impl Shell {
let maybe_window = workspace.focus_stack.get(seat).iter().next().cloned();
if let Some(window) = maybe_window {
if set.sticky_layer.mapped().any(|m| m == &window) {
set.sticky_layer
.toggle_stacking_focused(seat, workspace.focus_stack.get_mut(seat), &self.workspaces.stack_behavior)
set.sticky_layer.toggle_stacking_focused(
seat,
workspace.focus_stack.get_mut(seat),
&self.workspaces.stack_behavior,
)
} else if workspace.tiling_layer.mapped().any(|(m, _)| m == &window) {
workspace
.tiling_layer
.toggle_stacking_focused(seat, workspace.focus_stack.get_mut(seat), &self.workspaces.stack_behavior)
workspace.tiling_layer.toggle_stacking_focused(
seat,
workspace.focus_stack.get_mut(seat),
&self.workspaces.stack_behavior,
)
} else if workspace.floating_layer.mapped().any(|w| w == &window) {
workspace
.floating_layer
.toggle_stacking_focused(seat, workspace.focus_stack.get_mut(seat), &self.workspaces.stack_behavior)
workspace.floating_layer.toggle_stacking_focused(
seat,
workspace.focus_stack.get_mut(seat),
&self.workspaces.stack_behavior,
)
} else {
None
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/iced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ impl<P: Program + Send + 'static> IcedElementInternal<P> {
})
.collect::<Vec<_>>()
}

pub fn program_mut(&mut self) -> &mut P {
&mut self.state.program_mut().0
}
}

impl<P: Program + Send + 'static> PointerTarget<crate::state::State> for IcedElement<P> {
Expand Down

0 comments on commit 9a66d8c

Please sign in to comment.