Skip to content
Open
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
32 changes: 32 additions & 0 deletions crates/gpui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,38 @@ impl SystemWindowTabController {
}
}

/// Replace the tab group containing the given window with the platform-provided tabs.
pub fn sync_tab_group(cx: &mut App, id: WindowId, tabs: Vec<SystemWindowTab>) {
if tabs.is_empty() || !tabs.iter().any(|tab| tab.id == id) {
return;
}

let synced_tab_ids: FxHashSet<_> = tabs.iter().map(|tab| tab.id).collect();
let mut controller = cx.global_mut::<SystemWindowTabController>();

let existing_group_id = controller
.tab_groups
.iter()
.find_map(|(group_id, group_tabs)| {
group_tabs
.iter()
.any(|tab| tab.id == id)
.then_some(*group_id)
})
.unwrap_or_else(|| controller.tab_groups.keys().max().map_or(0, |id| id + 1));

controller.tab_groups.retain(|group_id, group_tabs| {
if *group_id == existing_group_id {
return true;
}

group_tabs.retain(|tab| !synced_tab_ids.contains(&tab.id));
!group_tabs.is_empty()
});

controller.tab_groups.insert(existing_group_id, tabs);
}

/// Remove a tab from a tab group.
pub fn remove_tab(cx: &mut App, id: WindowId) -> Option<SystemWindowTab> {
let mut controller = cx.global_mut::<SystemWindowTabController>();
Expand Down
8 changes: 6 additions & 2 deletions crates/gpui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1640,8 +1640,12 @@ impl Window {
let mut cx = cx.to_async();
Box::new(move || {
handle
.update(&mut cx, |_, _window, cx| {
SystemWindowTabController::merge_all_windows(cx, handle.window_id());
.update(&mut cx, |_, window, cx| {
if let Some(tabs) = window.platform_window.tabbed_windows() {
SystemWindowTabController::sync_tab_group(cx, handle.window_id(), tabs);
} else {
SystemWindowTabController::merge_all_windows(cx, handle.window_id());
}
})
.log_err();
})
Expand Down
16 changes: 10 additions & 6 deletions crates/gpui_macos/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,15 @@ unsafe impl Send for MacWindowState {}

pub(crate) struct MacWindow(Arc<Mutex<MacWindowState>>);

fn notify_windows_merged(window_state: Arc<Mutex<MacWindowState>>) {
let mut lock = window_state.as_ref().lock();
if let Some(mut callback) = lock.merge_all_windows_callback.take() {
drop(lock);
callback();
window_state.lock().merge_all_windows_callback = Some(callback);
}
}

impl MacWindow {
pub fn open(
handle: AnyWindowHandle,
Expand Down Expand Up @@ -3101,12 +3110,7 @@ extern "C" fn merge_all_windows(this: &Object, _: Sel, _: id) {
let _: () = msg_send![super(this, class!(NSWindow)), mergeAllWindows:nil];

let window_state = get_window_state(this);
let mut lock = window_state.as_ref().lock();
if let Some(mut callback) = lock.merge_all_windows_callback.take() {
drop(lock);
callback();
window_state.lock().merge_all_windows_callback = Some(callback);
}
notify_windows_merged(window_state);
}
}

Expand Down
Loading