Implement maximize/restore controls

Remove also resize from top and right of the window
This commit is contained in:
Francesco Abbate 2021-04-12 11:54:52 +02:00
parent 96a0ae802a
commit 46791aefe5
4 changed files with 48 additions and 6 deletions

View File

@ -3,10 +3,18 @@ local common = require "core.common"
local style = require "core.style"
local View = require "core.view"
local restore_command = {
symbol = "w", action = function() system.set_window_mode("normal") end
}
local maximize_command = {
symbol = "W", action = function() system.set_window_mode("maximized") end
}
local title_commands = {
{symbol = "_", action = function() system.set_window_mode("minimized") end},
{symbol = "w", action = function() system.set_window_mode("maximized") end},
{symbol = "W", action = function() core.quit() end},
maximize_command,
{symbol = "X", action = function() core.quit() end},
}
local TitleView = View:extend()
@ -28,6 +36,8 @@ function TitleView:update()
self.size.y = 0
end
TitleView.super.update(self)
local window_mode = system.get_window_mode()
title_commands[2] = window_mode == "maximized" and restore_command or maximize_command
local title_height = self.size.y
if core.window_borderless and title_height ~= core.hit_test_title_height then
local icon_w = style.icon_font:get_width("_")

Binary file not shown.

View File

@ -1,5 +1,5 @@
{
"name": "",
"name": "icons",
"css_prefix_text": "icon-",
"css_use_suffix": false,
"hinting": true,
@ -99,7 +99,7 @@
{
"uid": "7394501fc0b17cb7bda99538f92e26d6",
"css": "window-close",
"code": 87,
"code": 88,
"src": "fontawesome"
},
{
@ -107,6 +107,12 @@
"css": "menu-1",
"code": 77,
"src": "fontawesome"
},
{
"uid": "07f0832c07f3d9713fffb06c8bffa027",
"css": "window-maximize",
"code": 87,
"src": "fontawesome"
}
]
}

View File

@ -44,6 +44,9 @@ typedef struct HitTestInfo HitTestInfo;
static HitTestInfo window_hit_info[1] = {{0, 0}};
#define RESIZE_FROM_TOP 0
#define RESIZE_FROM_RIGHT 0
static SDL_HitTestResult SDLCALL hit_test(SDL_Window *window, const SDL_Point *pt, void *data) {
const HitTestInfo *hit_info = (HitTestInfo *) data;
const int resize_border = hit_info->resize_border;
@ -52,7 +55,10 @@ static SDL_HitTestResult SDLCALL hit_test(SDL_Window *window, const SDL_Point *p
SDL_GetWindowSize(window, &w, &h);
if (pt->y < hit_info->title_height && pt->y > hit_info->resize_border &&
if (pt->y < hit_info->title_height &&
#if RESIZE_FROM_TOP
pt->y > hit_info->resize_border &&
#endif
pt->x > resize_border && pt->x < w - controls_width) {
return SDL_HITTEST_DRAGGABLE;
}
@ -63,12 +69,16 @@ static SDL_HitTestResult SDLCALL hit_test(SDL_Window *window, const SDL_Point *p
if (pt->x < resize_border && pt->y < resize_border) {
REPORT_RESIZE_HIT(TOPLEFT);
#if RESIZE_FROM_TOP
} else if (pt->x > resize_border && pt->x < w - controls_width && pt->y < resize_border) {
REPORT_RESIZE_HIT(TOP);
#endif
} else if (pt->x > w - resize_border && pt->y < resize_border) {
REPORT_RESIZE_HIT(TOPRIGHT);
#if RESIZE_FROM_RIGHT
} else if (pt->x > w - resize_border && pt->y > resize_border && pt->y < h - resize_border) {
REPORT_RESIZE_HIT(RIGHT);
#endif
} else if (pt->x > w - resize_border && pt->y > h - resize_border) {
REPORT_RESIZE_HIT(BOTTOMRIGHT);
} else if (pt->x < w - resize_border && pt->x > resize_border && pt->y > h - resize_border) {
@ -298,6 +308,21 @@ static int f_window_has_focus(lua_State *L) {
}
static int f_get_window_mode(lua_State *L) {
unsigned flags = SDL_GetWindowFlags(window);
if (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
lua_pushstring(L, "fullscreen");
} else if (flags & SDL_WINDOW_MINIMIZED) {
lua_pushstring(L, "minimized");
} else if (flags & SDL_WINDOW_MAXIMIZED) {
lua_pushstring(L, "maximized");
} else {
lua_pushstring(L, "normal");
}
return 1;
}
static int f_show_fatal_error(lua_State *L) {
const char *title = luaL_checkstring(L, 1);
const char *msg = luaL_checkstring(L, 2);
@ -508,6 +533,7 @@ static const luaL_Reg lib[] = {
{ "set_cursor", f_set_cursor },
{ "set_window_title", f_set_window_title },
{ "set_window_mode", f_set_window_mode },
{ "get_window_mode", f_get_window_mode },
{ "set_window_bordered", f_set_window_bordered },
{ "set_window_hit_test", f_set_window_hit_test },
{ "get_window_size", f_get_window_size },