Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/wm/src/commands/monitor/add_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use crate::{
pub fn add_monitor(
native_monitor: NativeMonitor,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
) -> anyhow::Result<Monitor> {
// Create `Monitor` instance. This uses the working area of the monitor
// instead of the bounds of the display. The working area excludes
// taskbars and other reserved display space.
Expand All @@ -36,6 +35,14 @@ pub fn add_monitor(
added_monitor: monitor.to_dto()?,
});

Ok(monitor)
}

pub fn move_bounded_workspaces_to_new_monitor(
monitor: &Monitor,
state: &mut WmState,
config: &UserConfig,
) -> anyhow::Result<()> {
let bound_workspace_configs = config
.value
.workspaces
Expand All @@ -55,7 +62,7 @@ pub fn add_monitor(
// Move workspaces that should be bound to the newly added monitor.
move_workspace_to_monitor(
&existing_workspace,
&monitor,
monitor,
state,
config,
)?;
Expand All @@ -74,7 +81,7 @@ pub fn add_monitor(
// automatically prioritize bound workspace configs and fall back to the
// first available one if needed.
if monitor.child_count() == 0 {
activate_workspace(None, Some(monitor), state, config)?;
activate_workspace(None, Some(monitor.clone()), state, config)?;
}

Ok(())
Expand Down
24 changes: 16 additions & 8 deletions packages/wm/src/events/handle_display_settings_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use wm_platform::Platform;

use crate::{
commands::monitor::{
add_monitor, remove_monitor, sort_monitors, update_monitor,
add_monitor, move_bounded_workspaces_to_new_monitor, remove_monitor,
sort_monitors, update_monitor,
},
models::Monitor,
traits::{CommonGetters, PositionGetters, WindowGetters},
user_config::UserConfig,
wm_state::WmState,
Expand Down Expand Up @@ -84,14 +86,16 @@ pub fn handle_display_settings_changed(
}
}

let mut new_monitors: Vec<Monitor> = Vec::new();

for native_monitor in new_native_monitors {
match pending_monitors.first() {
Some(_) => {
let monitor = pending_monitors.remove(0);
update_monitor(&monitor, native_monitor, state)
}
// Add monitor if it doesn't exist in state.
None => add_monitor(native_monitor, state, config),
if pending_monitors.is_empty() {
let monitor = add_monitor(native_monitor, state)?;
new_monitors.push(monitor);
Ok(())
} else {
let monitor = pending_monitors.remove(0);
update_monitor(&monitor, native_monitor, state)
}?;
}

Expand All @@ -111,6 +115,10 @@ pub fn handle_display_settings_changed(
// Sort monitors by position.
sort_monitors(&state.root_container)?;

for new_monitor in new_monitors {
move_bounded_workspaces_to_new_monitor(&new_monitor, state, config)?;
}

for window in state.windows() {
// Display setting changes can spread windows out sporadically, so mark
// all windows as needing a DPI adjustment (just in case).
Expand Down
9 changes: 6 additions & 3 deletions packages/wm/src/wm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use wm_platform::{NativeMonitor, NativeWindow, Platform};

use crate::{
commands::{
container::set_focused_descendant, general::platform_sync,
monitor::add_monitor, window::manage_window,
container::set_focused_descendant,
general::platform_sync,
monitor::{add_monitor, move_bounded_workspaces_to_new_monitor},
window::manage_window,
},
models::{
Container, Monitor, RootContainer, WindowContainer, Workspace,
Expand Down Expand Up @@ -104,7 +106,8 @@ impl WmState {
// Create a monitor, and consequently a workspace, for each detected
// native monitor.
for native_monitor in Platform::sorted_monitors()? {
add_monitor(native_monitor, self, config)?;
let monitor = add_monitor(native_monitor, self)?;
move_bounded_workspaces_to_new_monitor(&monitor, self, config)?;
}

// Manage windows in reverse z-order (bottom to top). This helps to
Expand Down