Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] macOS: drag OS window by margin #3744

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions glfw/cocoa_window.m
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ CGDirectDisplayID displayIDForWindow(_GLFWwindow *w) {
}

static unsigned long long display_link_shutdown_timer = 0;
static NSEvent* lastMouseDownEvent = NULL;
#define DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL s_to_monotonic_t(30ll)

void
Expand Down Expand Up @@ -969,6 +970,7 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)event
- (void)mouseDown:(NSEvent *)event
{
if (!window) return;
lastMouseDownEvent = event;
_glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_LEFT,
GLFW_PRESS,
Expand Down Expand Up @@ -2692,6 +2694,12 @@ GLFWAPI void glfwCocoaRequestRenderFrame(GLFWwindow *w, GLFWcocoarenderframefun
return _glfwPlatformGetNativeKeyForKey(glfw_key);
}

GLFWAPI void
glfwPerformCocoaWindowDrag(GLFWwindow *handle) {
_GLFWwindow* window = (_GLFWwindow*) handle;
[window->ns.object performWindowDragWithEvent:lastMouseDownEvent];
}


//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
Expand Down
1 change: 1 addition & 0 deletions glfw/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def generate_wrappers(glfw_header: str) -> None:
GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReopen(GLFWapplicationshouldhandlereopenfun callback)
GLFWapplicationwillfinishlaunchingfun glfwSetApplicationWillFinishLaunching(GLFWapplicationwillfinishlaunchingfun callback)
uint32_t glfwGetCocoaKeyEquivalent(uint32_t glfw_key, int glfw_mods, int* cocoa_mods)
void glfwPerformCocoaWindowDrag(GLFWwindow *handle)
void glfwCocoaRequestRenderFrame(GLFWwindow *w, GLFWcocoarenderframefun callback)
void* glfwGetX11Display(void)
int32_t glfwGetX11Window(GLFWwindow* window)
Expand Down
2 changes: 1 addition & 1 deletion kitty/data-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void colorprofile_report_stack(ColorProfile*, unsigned int*, unsigned int*);

void set_mouse_cursor(MouseShape);
void enter_event(void);
void mouse_event(int, int, int);
void mouse_event(GLFWwindow*, int, int, int);
void focus_in_event(void);
void scroll_event(double, double, int, int);
void on_key_input(GLFWkeyevent *ev);
Expand Down
3 changes: 3 additions & 0 deletions kitty/glfw-wrapper.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions kitty/glfw-wrapper.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions kitty/glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ mouse_button_callback(GLFWwindow *w, int button, int action, int mods) {
global_state.callback_os_window->last_mouse_activity_at = now;
if (button >= 0 && (unsigned int)button < arraysz(global_state.callback_os_window->mouse_button_pressed)) {
global_state.callback_os_window->mouse_button_pressed[button] = action == GLFW_PRESS ? true : false;
if (is_window_ready_for_callbacks()) mouse_event(button, mods, action);
if (is_window_ready_for_callbacks()) mouse_event(w, button, mods, action);
}
request_tick_callback();
global_state.callback_os_window = NULL;
Expand All @@ -331,7 +331,7 @@ cursor_pos_callback(GLFWwindow *w, double x, double y) {
global_state.callback_os_window->cursor_blink_zero_time = now;
global_state.callback_os_window->mouse_x = x * global_state.callback_os_window->viewport_x_ratio;
global_state.callback_os_window->mouse_y = y * global_state.callback_os_window->viewport_y_ratio;
if (is_window_ready_for_callbacks()) mouse_event(-1, mods_at_last_key_or_button_event, -1);
if (is_window_ready_for_callbacks()) mouse_event(w, -1, mods_at_last_key_or_button_event, -1);
request_tick_callback();
global_state.callback_os_window = NULL;
}
Expand Down
36 changes: 3 additions & 33 deletions kitty/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,6 @@ contains_mouse(Window *w) {
return (w->visible && window_left(w) <= x && x <= window_right(w) && window_top(w) <= y && y <= window_bottom(w));
}

static inline double
distance_to_window(Window *w) {
double x = global_state.callback_os_window->mouse_x, y = global_state.callback_os_window->mouse_y;
double cx = (window_left(w) + window_right(w)) / 2.0;
double cy = (window_top(w) + window_bottom(w)) / 2.0;
return (x - cx) * (x - cx) + (y - cy) * (y - cy);
}

static bool clamp_to_window = false;

static inline bool
Expand Down Expand Up @@ -529,23 +521,6 @@ window_for_event(unsigned int *window_idx, bool *in_tab_bar) {
return NULL;
}

static inline Window*
closest_window_for_event(unsigned int *window_idx) {
Window *ans = NULL;
double closest_distance = UINT_MAX;
if (global_state.callback_os_window->num_tabs > 0) {
Tab *t = global_state.callback_os_window->tabs + global_state.callback_os_window->active_tab;
for (unsigned int i = 0; i < t->num_windows; i++) {
Window *w = t->windows + i;
if (w->visible) {
double d = distance_to_window(w);
if (d < closest_distance) { ans = w; closest_distance = d; *window_idx = i; }
}
}
}
return ans;
}

void
focus_in_event() {
// Ensure that no URL is highlighted and the mouse cursor is in default shape
Expand Down Expand Up @@ -629,7 +604,7 @@ mouse_selection(Window *w, int code, int button) {


void
mouse_event(int button, int modifiers, int action) {
mouse_event(GLFWwindow *window, int button, int modifiers, int action) {
MouseShape old_cursor = mouse_cursor_shape;
bool in_tab_bar;
unsigned int window_idx = 0;
Expand Down Expand Up @@ -671,13 +646,8 @@ mouse_event(int button, int modifiers, int action) {
handle_event(w, button, modifiers, window_idx);
} else if (button == GLFW_MOUSE_BUTTON_LEFT && global_state.callback_os_window->mouse_button_pressed[button]) {
// initial click, clamp it to the closest window
w = closest_window_for_event(&window_idx);
if (w) {
clamp_to_window = true;
debug("grabbed: %d\n", w->render_data.screen->modes.mouse_tracking_mode != 0);
handle_event(w, button, modifiers, window_idx);
clamp_to_window = false;
} else debug("no window for event\n");
debug("start window drag move\n");
glfwPerformCocoaWindowDrag(window);
} else debug("\n");
if (mouse_cursor_shape != old_cursor) {
set_mouse_cursor(mouse_cursor_shape);
Expand Down