Merge pull request #239 from emersion/center-views

Center views in rootston
This commit is contained in:
Drew DeVault 2017-10-08 12:51:42 -04:00 committed by GitHub
commit 8dcf3bfdf7
6 changed files with 84 additions and 8 deletions

View File

@ -132,4 +132,7 @@ void view_begin_move(struct roots_input *input, struct wlr_cursor *cursor,
void view_begin_resize(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view, uint32_t edges);
void set_view_focus(struct roots_input *input, struct roots_desktop *desktop,
struct roots_view *view);
#endif

View File

@ -1,5 +1,6 @@
#ifndef _ROOTSTON_VIEW_H
#define _ROOTSTON_VIEW_H
#include <stdbool.h>
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_surface.h>
@ -14,17 +15,25 @@ struct roots_wl_shell_surface {
struct wl_listener request_resize;
struct wl_listener request_set_fullscreen;
struct wl_listener request_set_maximized;
struct wl_listener surface_commit;
bool initialized;
};
struct roots_xdg_surface_v6 {
struct roots_view *view;
// TODO: Maybe destroy listener should go in roots_view
struct wl_listener commit;
struct wl_listener destroy;
struct wl_listener ping_timeout;
struct wl_listener request_minimize;
struct wl_listener request_move;
struct wl_listener request_resize;
struct wl_listener request_show_window_menu;
bool initialized;
};
struct roots_xwayland_surface {
@ -73,5 +82,7 @@ void view_get_input_bounds(struct roots_view *view, struct wlr_box *box);
void view_activate(struct roots_view *view, bool active);
void view_resize(struct roots_view *view, uint32_t width, uint32_t height);
void view_close(struct roots_view *view);
bool view_center(struct roots_view *view);
bool view_initialize(struct roots_view *view);
#endif

View File

@ -123,8 +123,8 @@ void cursor_update_position(struct roots_input *input, uint32_t time) {
}
}
static void set_view_focus(struct roots_input *input,
struct roots_desktop *desktop, struct roots_view *view) {
void set_view_focus(struct roots_input *input, struct roots_desktop *desktop,
struct roots_view *view) {
if (input->active_view == view) {
return;
}
@ -140,8 +140,7 @@ static void set_view_focus(struct roots_input *input,
struct roots_view *_view = desktop->views->items[i];
if (_view != view) {
view_activate(_view, false);
}
if (view == _view) {
} else {
index = i;
}
}

View File

@ -83,6 +83,36 @@ void view_close(struct roots_view *view) {
}
}
bool view_center(struct roots_view *view) {
struct wlr_box size;
view_get_size(view, &size);
if (size.width == 0 && size.height == 0) {
return false;
}
struct roots_desktop *desktop = view->desktop;
struct wlr_cursor *cursor = desktop->server->input->cursor;
struct wlr_output *output = wlr_output_layout_output_at(desktop->layout,
cursor->x, cursor->y);
if (!output) {
return false;
}
view->x = (double)(output->width - size.width) / 2;
view->y = (double)(output->height - size.height) / 2;
return true;
}
bool view_initialize(struct roots_view *view) {
bool centered = view_center(view);
if (centered) {
struct roots_input *input = view->desktop->server->input;
set_view_focus(input, view->desktop, view);
wlr_seat_keyboard_notify_enter(input->wl_seat, view->wlr_surface);
}
return centered;
}
static struct wlr_subsurface *subsurface_at(struct wlr_surface *surface,
double sx, double sy, double *sub_x, double *sub_y) {
struct wlr_subsurface *subsurface;

View File

@ -49,6 +49,17 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
view_begin_resize(input, event->cursor, view, e->edges);
}
static void handle_surface_commit(struct wl_listener *listener, void *data) {
struct roots_wl_shell_surface *roots_surface =
wl_container_of(listener, roots_surface, surface_commit);
struct roots_view *view = roots_surface->view;
if (view->wl_shell_surface->state == WLR_WL_SHELL_SURFACE_STATE_TOPLEVEL &&
!roots_surface->initialized) {
roots_surface->initialized = view_initialize(view);
}
}
static void handle_destroy(struct wl_listener *listener, void *data) {
struct roots_wl_shell_surface *roots_surface =
wl_container_of(listener, roots_surface, destroy);
@ -73,7 +84,9 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) {
struct roots_wl_shell_surface *roots_surface =
calloc(1, sizeof(struct roots_wl_shell_surface));
// TODO: all of the trimmings
if (!roots_surface) {
return;
}
wl_list_init(&roots_surface->destroy.link);
roots_surface->destroy.notify = handle_destroy;
wl_signal_add(&surface->events.destroy, &roots_surface->destroy);
@ -83,9 +96,14 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) {
wl_signal_add(&surface->events.request_move, &roots_surface->request_move);
wl_list_init(&roots_surface->request_resize.link);
roots_surface->request_resize.notify = handle_request_resize;
wl_signal_add(&surface->events.request_resize, &roots_surface->request_resize);
wl_signal_add(&surface->events.request_resize,
&roots_surface->request_resize);
wl_list_init(&roots_surface->request_set_fullscreen.link);
wl_list_init(&roots_surface->request_set_maximized.link);
wl_list_init(&roots_surface->surface_commit.link);
roots_surface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&surface->surface->signals.commit,
&roots_surface->surface_commit);
struct roots_view *view = calloc(1, sizeof(struct roots_view));
view->type = ROOTS_WL_SHELL_VIEW;

View File

@ -67,6 +67,17 @@ static void handle_request_resize(struct wl_listener *listener, void *data) {
view_begin_resize(input, event->cursor, view, e->edges);
}
static void handle_commit(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, commit);
struct roots_view *view = roots_xdg_surface->view;
if (view->xdg_surface_v6->role == WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL &&
!roots_xdg_surface->initialized) {
roots_xdg_surface->initialized = view_initialize(view);
}
}
static void handle_destroy(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, destroy);
@ -98,7 +109,12 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_surface =
calloc(1, sizeof(struct roots_xdg_surface_v6));
// TODO: all of the trimmings
if (!roots_surface) {
return;
}
wl_list_init(&roots_surface->commit.link);
roots_surface->commit.notify = handle_commit;
wl_signal_add(&surface->events.commit, &roots_surface->commit);
wl_list_init(&roots_surface->destroy.link);
roots_surface->destroy.notify = handle_destroy;
wl_signal_add(&surface->events.destroy, &roots_surface->destroy);
@ -115,7 +131,6 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
struct roots_view *view = calloc(1, sizeof(struct roots_view));
view->type = ROOTS_XDG_SHELL_V6_VIEW;
view->x = view->y = 200;
view->xdg_surface_v6 = surface;
view->roots_xdg_surface_v6 = roots_surface;
view->wlr_surface = surface->surface;