From 91db168547984311edbe4fc285afd14116dd5bd2 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 26 Jan 2025 15:05:45 +0100 Subject: [PATCH] only select items when the highlight rect is in the drag area --- mizunara/gtk-filesview.c | 140 +++++++++++++++++++++++++++++---------- mizunara/window.c | 4 +- 2 files changed, 109 insertions(+), 35 deletions(-) diff --git a/mizunara/gtk-filesview.c b/mizunara/gtk-filesview.c index e86e72a..cf13919 100644 --- a/mizunara/gtk-filesview.c +++ b/mizunara/gtk-filesview.c @@ -92,6 +92,9 @@ MzFilesView* mz_files_view_new(void) { #define MZ_GRID_VIEW_PADDING_LEFT 10 #define MZ_GRID_VIEW_PADDING_RIGHT 10 +#define MZ_GRID_VIEW_SELECTION_WIDTH 165 +#define MZ_GRID_VIEW_SELECTION_HEIGHT 165 + void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { MzFilesView *view = (MzFilesView*)widget; //printf("MzFilesView snapshot\n"); @@ -104,7 +107,20 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { width -= MZ_GRID_VIEW_PADDING_LEFT + MZ_GRID_VIEW_PADDING_RIGHT; - int item_width = 170; + int drag_x = view->drag_start_x; + int drag_y = view->drag_start_y; + int drag_w = view->drag_width; + int drag_h = view->drag_height; + if(drag_w < 0) { + drag_x += drag_w; + drag_w = -drag_w; + } + if(drag_h < 0) { + drag_y += drag_h; + drag_h = -drag_h; + } + + int item_width = 180; int item_height = 170; int items_per_line = width / item_width; @@ -128,26 +144,68 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { gdk_rgba_parse(&highlight_color, "#0a0a0a0a"); GdkRGBA selection_color; - gdk_rgba_parse(&selection_color, "#2f0a0a1f"); + gdk_rgba_parse(&selection_color, "#0a0a0a1f"); graphene_rect_t highlight_rect; - highlight_rect.size.width = item_width-5; - highlight_rect.size.height = item_height-5; + highlight_rect.size.width = MZ_GRID_VIEW_SELECTION_WIDTH; + highlight_rect.size.height = MZ_GRID_VIEW_SELECTION_HEIGHT; + int highlight_rect_offset_x = (item_width - highlight_rect.size.width) / 2; + int highlight_rect_offset_y = (item_height - highlight_rect.size.height) / 2; + + GskRoundedRect highlight_clip; + highlight_clip.bounds = GRAPHENE_RECT_INIT(0, 0, highlight_rect.size.width, highlight_rect.size.height); + for(int i=0;i<4;i++) { + highlight_clip.corner[i].width = 16; + highlight_clip.corner[i].height = 16; + } + + int handle_selection = view->drag || view->update_selection; + + if(highlight_col_start >= 0) { + // check minimum pos + int col_start_pos = MZ_GRID_VIEW_PADDING_LEFT + highlight_col_start*item_width + highlight_rect_offset_x; + if(drag_x < col_start_pos && drag_x + drag_w < col_start_pos) { + highlight_col_start++; + } else if(drag_x >= col_start_pos + highlight_rect.size.width) { + highlight_col_start++; + } + } + if(highlight_col_end >= 0) { + int col_start_pos = MZ_GRID_VIEW_PADDING_LEFT + highlight_col_end*item_width + highlight_rect_offset_x; + if(drag_x+drag_w < col_start_pos) { + highlight_col_end--; + } + } + if(highlight_row_start >= 0) { + // check minimum pos + int row_start_pos = MZ_GRID_VIEW_PADDING_TOP + highlight_row_start*item_height + highlight_rect_offset_y; + if(drag_y < row_start_pos && drag_y + drag_h < row_start_pos) { + highlight_row_start++; + } else if(drag_y >= row_start_pos + highlight_rect.size.height) { + highlight_row_start++; + } + } + if(highlight_row_end >= 0) { + int row_start_pos = MZ_GRID_VIEW_PADDING_TOP + highlight_row_end*item_height + highlight_rect_offset_y; + if(drag_y+drag_w < row_start_pos) { + highlight_row_end--; + } + } if(view->numitems > 0) { GtkAllocation img_alloc; GtkAllocation label_alloc; img_alloc.width = 100; img_alloc.height = 100; - label_alloc.width = 160; + label_alloc.width = 140; label_alloc.height = 60; int img_x_offset = (item_width - img_alloc.width) / 2; int img_y_offset = (item_height - img_alloc.height - label_alloc.height) / 2; int label_x_offset = (item_width - label_alloc.width) / 2; - int handle_selection = view->drag || view->update_selection; + int x = 0; int y = 0; for(size_t i=0;inumitems;i++) { @@ -155,9 +213,17 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { int base_y = y * item_height + MZ_GRID_VIEW_PADDING_TOP; if(handle_selection && x >= highlight_col_start && x <= highlight_col_end && y >= highlight_row_start && y <= highlight_row_end) { if(view->drag) { - highlight_rect.origin.x = base_x + 5; - highlight_rect.origin.y = base_y + 5; - gtk_snapshot_append_color(snapshot, &highlight_color, &highlight_rect); + highlight_rect.origin.x = base_x + highlight_rect_offset_x; + highlight_rect.origin.y = base_y + highlight_rect_offset_y; + + highlight_clip.bounds.origin.x = highlight_rect.origin.x; + highlight_clip.bounds.origin.y = highlight_rect.origin.y; + + GskRenderNode *highlight_node = gsk_color_node_new( + &highlight_color, + &highlight_rect); + GskRenderNode *highlight_clip_node = gsk_rounded_clip_node_new(highlight_node, &highlight_clip); + gtk_snapshot_append_node(snapshot, highlight_clip_node); } else { view->items[i].isselected = 1; } @@ -166,9 +232,17 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { } if(view->items[i].isselected) { - highlight_rect.origin.x = base_x + 5; - highlight_rect.origin.y = base_y + 5; - gtk_snapshot_append_color(snapshot, &selection_color, &highlight_rect); + highlight_rect.origin.x = base_x + highlight_rect_offset_x; + highlight_rect.origin.y = base_y + highlight_rect_offset_y; + + highlight_clip.bounds.origin.x = highlight_rect.origin.x; + highlight_clip.bounds.origin.y = highlight_rect.origin.y; + + GskRenderNode *highlight_node = gsk_color_node_new( + &selection_color, + &highlight_rect); + GskRenderNode *highlight_clip_node = gsk_rounded_clip_node_new(highlight_node, &highlight_clip); + gtk_snapshot_append_node(snapshot, highlight_clip_node); } img_alloc.x = MZ_GRID_VIEW_PADDING_LEFT + img_x_offset + x * item_width; @@ -179,8 +253,8 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { gtk_widget_size_allocate(view->items[i].label, &label_alloc, -1); PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(view->items[i].label)); - pango_layout_set_width(layout, 160 * PANGO_SCALE); - pango_layout_set_height(layout, 60 * PANGO_SCALE); + pango_layout_set_width(layout, label_alloc.width * PANGO_SCALE); + pango_layout_set_height(layout, label_alloc.height * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE); pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); @@ -204,21 +278,8 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { gdk_rgba_parse(&fill_color, "#3333331f"); gdk_rgba_parse(&border_color, "#1111114f"); - int x = view->drag_start_x; - int y = view->drag_start_y; - int w = view->drag_width; - int h = view->drag_height; - if(w < 0) { - x += w; - w = -w; - } - if(h < 0) { - y += h; - h = -h; - } - GskRoundedRect rect; - rect.bounds = GRAPHENE_RECT_INIT(x, y, w, h); + rect.bounds = GRAPHENE_RECT_INIT(drag_x, drag_y, drag_w, drag_h); for(int i=0;i<4;i++) { rect.corner[i].width = 8; rect.corner[i].height = 8; @@ -264,7 +325,7 @@ void mz_files_view_measure(GtkWidget *widget, } if(orientation == GTK_ORIENTATION_VERTICAL) { - int item_width = 170; + int item_width = 180; int item_height = 170; int items_per_line = (view->current_width - MZ_GRID_VIEW_PADDING_LEFT - MZ_GRID_VIEW_PADDING_RIGHT) / item_width; @@ -307,17 +368,22 @@ static void drag_begin_cb( gpointer user_data) { MzFilesView *view = user_data; - printf("drag_begin_cb\n"); + printf("drag_begin_cb: %f\n", start_x); // update selection int item_selection_width = 160; int item_selection_height = 160; int selection_offset_x = 5; int selection_offset_y = 5; - int item_width = 170; + int item_width = 180; int item_height = 170; - int col = start_x / item_width; + int col; + if(start_x <= MZ_GRID_VIEW_PADDING_LEFT) { + col = -1; + } else { + col = (start_x - MZ_GRID_VIEW_PADDING_LEFT) / item_width; + } int row = start_y / item_height; view->drag = TRUE; @@ -361,10 +427,16 @@ static void drag_update_cb( int item_selection_height = 160; int selection_offset_x = 5; int selection_offset_y = 5; - int item_width = 170; + int item_width = 180; int item_height = 170; - int col = (view->drag_start_x + x) / item_width; + int col; + if(view->drag_start_x + x <= MZ_GRID_VIEW_PADDING_LEFT) { + col = -1; + } else { + col = (view->drag_start_x + x - MZ_GRID_VIEW_PADDING_LEFT) / item_width; + } + int row = (view->drag_start_y + y) / item_height; view->drag_col = col; view->drag_row = row; diff --git a/mizunara/window.c b/mizunara/window.c index 3b41a00..67f7fdc 100644 --- a/mizunara/window.c +++ b/mizunara/window.c @@ -89,7 +89,9 @@ UiObject* window_create(const char *url) { window_create_browser_view(obj, wdata); ui_set(browser->view, 0); // select default view - + // test + ui_set(browser->path, "/usr"); + filebrowser_load(browser, "/usr"); return obj; } -- 2.43.5