From 15cc4fa985c1d6adc78c68a90fe90ba3d219e221 Mon Sep 17 00:00:00 2001
From: longcheng <longcheng@kylinos.com.cn>
Date: Tue, 22 Mar 2022 16:51:12 +0800
Subject: [PATCH 0/4] draw mask for whole screen except the selection area

---
 mate-screenshot/src/screenshot-utils.c | 79 ++++++++++----------------
 1 file changed, 30 insertions(+), 49 deletions(-)

diff --git a/mate-screenshot/src/screenshot-utils.c b/mate-screenshot/src/screenshot-utils.c
index 8809571..a8eaea7 100644
--- a/mate-screenshot/src/screenshot-utils.c
+++ b/mate-screenshot/src/screenshot-utils.c
@@ -213,6 +213,10 @@ select_area_button_press (GtkWidget               *window,
   data->button_pressed = TRUE;
   data->rect.x = event->x_root;
   data->rect.y = event->y_root;
+  data->rect.width = 0;
+  data->rect.height = 0;
+
+  gtk_widget_queue_draw(window);
 
   return TRUE;
 }
@@ -227,52 +231,10 @@ select_area_motion_notify (GtkWidget               *window,
   if (!data->button_pressed)
     return TRUE;
 
-  draw_rect.width = ABS (data->rect.x - event->x_root);
-  draw_rect.height = ABS (data->rect.y - event->y_root);
-  draw_rect.x = MIN (data->rect.x, event->x_root);
-  draw_rect.y = MIN (data->rect.y, event->y_root);
-
-  if (draw_rect.width <= 0 || draw_rect.height <= 0)
-    {
-      gtk_window_move (GTK_WINDOW (window), -100, -100);
-      gtk_window_resize (GTK_WINDOW (window), 10, 10);
-      return TRUE;
-    }
-
-  gtk_window_move (GTK_WINDOW (window), draw_rect.x, draw_rect.y);
-  gtk_window_resize (GTK_WINDOW (window), draw_rect.width, draw_rect.height);
-
-  /* We (ab)use app-paintable to indicate if we have an RGBA window */
-  if (!gtk_widget_get_app_paintable (window))
-    {
-      GdkWindow *gdkwindow = gtk_widget_get_window (window);
-
-      /* Shape the window to make only the outline visible */
-      if (draw_rect.width > 2 && draw_rect.height > 2)
-        {
-          cairo_region_t *region, *region2;
-          cairo_rectangle_int_t region_rect = {
-	    0, 0,
-            draw_rect.width - 2, draw_rect.height - 2
-          };
-
-          region = cairo_region_create_rectangle (&region_rect);
-          region_rect.x++;
-          region_rect.y++;
-          region_rect.width -= 2;
-          region_rect.height -= 2;
-          region2 = cairo_region_create_rectangle (&region_rect);
-          cairo_region_subtract (region, region2);
-
-          gdk_window_shape_combine_region (gdkwindow, region, 0, 0);
-
-          cairo_region_destroy (region);
-          cairo_region_destroy (region2);
-        }
-      else
-        gdk_window_shape_combine_region (gdkwindow, NULL, 0, 0);
-    }
+  data->rect.width = event->x_root - data->rect.x;
+  data->rect.height = event->y_root - data->rect.y;
 
+  gtk_widget_queue_draw(window);
   return TRUE;
 }
 
@@ -316,18 +278,35 @@ static gboolean
 draw (GtkWidget *window, cairo_t *cr, gpointer unused)
 {
   GtkStyleContext *style;
+  GdkRectangle *rect;
 
   style = gtk_widget_get_style_context (window);
 
   if (gtk_widget_get_app_paintable (window))
     {
       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
-      cairo_set_source_rgba (cr, 0, 0, 0, 0);
+      cairo_set_source_rgba (cr, 0, 0, 0, 0.6);
       cairo_paint (cr);
 
       gtk_style_context_save (style);
       gtk_style_context_add_class (style, GTK_STYLE_CLASS_RUBBERBAND);
 
+      cairo_save(cr);
+      rect = g_object_get_data(G_OBJECT(window), "selection-area");
+      if (rect->width != 0 || rect->height != 0) {
+             cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+             cairo_set_source_rgba (cr, 0.0, 0, 0, 0.0);
+             cairo_rectangle(cr, rect->x, rect->y, rect->width, rect->height);
+             cairo_fill (cr);
+
+             cairo_set_line_width (cr, 1.0);
+             cairo_set_source_rgb (cr, 255/255.0, 0/255.0, 0/255.0);
+             cairo_rectangle (cr,
+                             rect->x + 0.5, rect->y + 0.5,
+                             rect->width - 1, rect->height - 1);
+             cairo_stroke (cr);
+      }
+#if 0
       gtk_render_background (style, cr,
                              0, 0,
                              gtk_widget_get_allocated_width (window),
@@ -336,7 +315,8 @@ draw (GtkWidget *window, cairo_t *cr, gpointer unused)
                         0, 0,
                         gtk_widget_get_allocated_width (window),
                         gtk_widget_get_allocated_height (window));
-
+#endif
+      cairo_restore(cr);
       gtk_style_context_restore (style);
     }
 
@@ -358,8 +338,8 @@ create_select_window (void)
 
   g_signal_connect (window, "draw", G_CALLBACK (draw), NULL);
 
-  gtk_window_move (GTK_WINDOW (window), -100, -100);
-  gtk_window_resize (GTK_WINDOW (window), 10, 10);
+  gtk_window_resize(GTK_WINDOW(window), gdk_screen_get_width(screen), gdk_screen_get_height(screen));
+  gtk_window_move(GTK_WINDOW(window), 0, 0);
   gtk_widget_show (window);
   return window;
 }
@@ -401,6 +381,7 @@ screenshot_select_area_async (SelectAreaCallback callback)
   cb_data = g_slice_new0 (CallbackData);
   cb_data->callback = callback;
 
+  g_object_set_data(G_OBJECT(data.window), "selection-area", &data.rect);
   g_signal_connect (data.window, "key-press-event", G_CALLBACK (select_area_key_press), &data);
   g_signal_connect (data.window, "button-press-event", G_CALLBACK (select_area_button_press), &data);
   g_signal_connect (data.window, "button-release-event", G_CALLBACK (select_area_button_release), &data);
-- 
2.27.0

