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
5 changes: 5 additions & 0 deletions crates/refryt/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,8 @@ pub struct App {
/// macOS: the system's window buttons, kept in the shell's top-left corner. A no-op
/// elsewhere.
traffic_lights: crate::chrome::TrafficLights,
/// Turns macOS's native title-bar drag off over controls; see `chrome::NativeDrag`.
native_drag: crate::chrome::NativeDrag,
/// Whether the sidebar was drawn folded to the icon rail last frame, for placing the traffic
/// lights over it.
pub(crate) sidebar_rail: bool,
Expand Down Expand Up @@ -2162,6 +2164,7 @@ impl App {
#[cfg(target_os = "linux")]
let activation = crate::activation::Activation::new(cc);
let traffic_lights = crate::chrome::TrafficLights::new(cc);
let native_drag = crate::chrome::NativeDrag::new(cc, decorated);
// Until the sidebar is first drawn (the splash comes first), from its saved width.
let sidebar_rail = settings.sidebar_width.is_some_and(theme::sidebar_is_rail);

Expand Down Expand Up @@ -2425,6 +2428,7 @@ impl App {
#[cfg(target_os = "linux")]
activation,
traffic_lights,
native_drag,
sidebar_rail,
traffic_lights_shown: false,
media,
Expand Down Expand Up @@ -9019,6 +9023,7 @@ impl eframe::App for App {
self.place_traffic_lights(fullscreen || player_fullscreen);

chrome::paint_background(ui, &p, rounded);
self.native_drag.update(&ctx);

// Ask KWin to blur what the transparency shows through. A no-op call every frame once
// the size and state settle — `Blur::set` compares against what it last pushed and only
Expand Down
102 changes: 97 additions & 5 deletions crates/refryt/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ pub fn drag_surface(ui: &Ui, rect: Rect, decorated: bool, id_salt: &str) {
return;
}
let ctx = ui.ctx().clone();
let drag = ui.interact(
rect,
egui::Id::new("ytm-titlebar-drag").with(id_salt),
Sense::click_and_drag(),
);
let id = egui::Id::new("ytm-titlebar-drag").with(id_salt);
// Remembered so [`NativeDrag`] can tell these surfaces from the controls drawn over them.
ctx.data_mut(|d| {
d.get_temp_mut_or_default::<egui::IdSet>(drag_surfaces_id())
.insert(id);
});
let drag = ui.interact(rect, id, Sense::click_and_drag());
if drag.drag_started() {
ctx.send_viewport_cmd(ViewportCommand::StartDrag);
hand_off_pointer();
Expand All @@ -206,6 +208,96 @@ pub fn drag_surface(ui: &Ui, rect: Rect, decorated: bool, id_salt: &str) {
}
}

/// Where [`drag_surface`] keeps the ids of every surface it has drawn. They are stable, and
/// there are only a handful, so the set is never cleared.
fn drag_surfaces_id() -> egui::Id {
egui::Id::new("ytm-titlebar-drag-surfaces")
}

/// Keeps macOS's own title-bar drag off the controls drawn over it.
///
/// The macOS window keeps a native title bar, only hidden, with the content running under it
/// (`main.rs`). AppKit still moves the window on a press anywhere in that hidden strip, before
/// egui sees the event, so it ignores whatever egui drew there: drag-selecting text in the
/// search box moved the window instead. Each frame this reads egui's hit test and lets the
/// window be moved natively only where a press would land on nothing, or on one of
/// [`drag_surface`]'s surfaces. Empty title-bar space drags as before; the controls get their
/// presses.
///
/// A no-op on every other platform and with `--decorated`, where the title bar is the OS's own.
#[derive(Default)]
pub struct NativeDrag {
#[cfg(target_os = "macos")]
view: Option<objc2::rc::Retained<objc2_app_kit::NSView>>,
#[cfg(target_os = "macos")]
movable: Option<bool>,
}

impl NativeDrag {
pub fn new(handles: &impl raw_window_handle::HasWindowHandle, decorated: bool) -> Self {
#[cfg(target_os = "macos")]
{
use raw_window_handle::RawWindowHandle;
if decorated {
return Self::default();
}
let view = handles.window_handle().ok().and_then(|h| match h.as_raw() {
// SAFETY: eframe hands out the window's live content `NSView`, on the main
// thread, while the window exists.
RawWindowHandle::AppKit(handle) => unsafe {
objc2::rc::Retained::retain(
handle.ns_view.as_ptr().cast::<objc2_app_kit::NSView>(),
)
},
_ => None,
});
Self {
view,
movable: None,
}
}
#[cfg(not(target_os = "macos"))]
{
let _ = (handles, decorated);
Self {}
}
}

/// Call once a frame. AppKit is only told when the answer changes.
pub fn update(&mut self, ctx: &egui::Context) {
#[cfg(target_os = "macos")]
{
let Some(view) = &self.view else {
return;
};
let movable = native_drag_allowed(ctx);
if self.movable == Some(movable) {
return;
}
let Some(window) = view.window() else {
return;
};
window.setMovable(movable);
self.movable = Some(movable);
}
#[cfg(not(target_os = "macos"))]
let _ = ctx;
}
}

/// Whether a press under the pointer may move the window: no widget would take it, or only a
/// title-bar drag surface would.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn native_drag_allowed(ctx: &egui::Context) -> bool {
let surfaces = ctx.data(|d| d.get_temp::<egui::IdSet>(drag_surfaces_id()));
ctx.viewport(|vp| {
[&vp.hits.click, &vp.hits.drag]
.into_iter()
.flatten()
.all(|w| surfaces.as_ref().is_some_and(|s| s.contains(&w.id)))
})
}

/// Minimise, maximise and close, for the current layout's leading edge.
///
/// Drawn into whatever `Ui` is passed, so the caller decides where they sit; in practice that is
Expand Down
8 changes: 7 additions & 1 deletion crates/refryt/src/ui/topbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ fn history_button(
/// The search box as a pill with the magnifier inside it, rather than egui's default field.
fn search_field(app: &mut App, ui: &mut Ui, width: f32) {
let p = app.palette;
let (rect, response) = ui.allocate_exact_size(vec2(width, SEARCH_H), Sense::hover());
// The whole pill takes the press, not just the text row inside it: with hover only, a press
// on the padding or the magnifier fell through to the bar's drag surface underneath and
// moved the window. A click there focuses the field, as it would on the text itself.
let (rect, response) = ui.allocate_exact_size(vec2(width, SEARCH_H), Sense::click_and_drag());
if response.clicked() || response.drag_started() {
ui.memory_mut(|m| m.request_focus(search_id()));
}
let focused = ui.memory(|m| m.has_focus(search_id()));
let t = widgets::lit(ui, &response).max(if focused { 1.0 } else { 0.0 });

Expand Down
Loading