Skip to content

Commit

Permalink
Remove global and extern variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronayub committed Sep 24, 2024
1 parent 8a20e66 commit 14570dc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
39 changes: 20 additions & 19 deletions src/di-app-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ struct _DiAppWindow {
GtkWidget *allFilesContainer;
GtkWidget *allFilesLabel;
GtkWidget *allFilesImage;
gboolean allowQuit;
gboolean autoclose;
};

/* If allowQuit and autoclose are true, drag/drop events outside the application
* will exit the application. */
extern gboolean autoclose;
gboolean allowQuit = false;

G_DEFINE_TYPE (DiAppWindow, di_app_window, GTK_TYPE_APPLICATION_WINDOW)


static void motion_enter (void) {
allowQuit = false;
static void motion_enter (GtkEventControllerMotion *self, gdouble x, gdouble y, gpointer user_data) {
DiAppWindow *win = DI_APP_WINDOW (user_data);
win->allowQuit = FALSE;
}
static void motion_leave (void) {
allowQuit = true;
static void motion_leave (GtkEventControllerMotion *self, gdouble x, gdouble y, gpointer user_data) {
DiAppWindow *win = DI_APP_WINDOW (user_data);
win->allowQuit = TRUE;
}

/** Exit the application if a drag event ends within the application window. */
void drag_end_cb (void) {
if (autoclose && allowQuit) {
exit (0);
/** Exit the application if a drag event ends within the application window
* and the user has the autoclose option set. */
void drag_end_cb (GtkDragSource *self, GdkDrag *drag, gboolean delete_data, gpointer user_data) {
DiAppWindow *win = DI_APP_WINDOW (user_data);
if (win->autoclose && win->allowQuit) {
gtk_window_close (GTK_WINDOW (win));
}
}

Expand All @@ -60,10 +60,11 @@ DiAppWindow *di_app_window_new (GtkApplication *app) {
}

/** Set up the window by loading all valid files */
void di_app_window_open (DiAppWindow *win, GFile **files, int n_files) {
void di_app_window_open (DiAppWindow *win, gboolean autoclose, GFile **files, int n_files) {
GtkIconTheme *iconTheme;
GtkIconPaintable *iconPaintable;
const char *iconName;
win->autoclose = autoclose;

int validFiles = 0; // Number of files which exist

Expand All @@ -73,7 +74,7 @@ void di_app_window_open (DiAppWindow *win, GFile **files, int n_files) {

validFiles++;
fileCell = di_file_cell_new ();
di_file_cell_load (fileCell, files[i]);
di_file_cell_load (fileCell, files[i], win);
gtk_flow_box_append (GTK_FLOW_BOX (win->box), GTK_WIDGET (fileCell));
}
}
Expand Down Expand Up @@ -104,13 +105,13 @@ void di_app_window_open (DiAppWindow *win, GFile **files, int n_files) {
gtk_drag_source_set_content (dsource, contentProvider);
gtk_drag_source_set_icon (dsource, GDK_PAINTABLE (iconPaintable), 0, 0);
g_object_unref (contentProvider);
g_signal_connect (dsource, "drag-end", G_CALLBACK (drag_end_cb), NULL);
g_signal_connect (dsource, "drag-end", G_CALLBACK (drag_end_cb), win);
gtk_widget_add_controller (GTK_WIDGET (win->allFilesContainer), GTK_EVENT_CONTROLLER (dsource));

// Set up event controller for the window
GtkEventController *controller = gtk_drop_controller_motion_new ();
g_signal_connect (controller, "enter", G_CALLBACK (motion_enter), NULL);
g_signal_connect (controller, "leave", G_CALLBACK (motion_leave), NULL);
g_signal_connect (controller, "enter", G_CALLBACK (motion_enter), win);
g_signal_connect (controller, "leave", G_CALLBACK (motion_leave), win);
gtk_widget_add_controller (GTK_WIDGET (win->mainContainer), GTK_EVENT_CONTROLLER (controller));
}

4 changes: 2 additions & 2 deletions src/di-app-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ G_DECLARE_FINAL_TYPE (DiAppWindow, di_app_window, DI, APP_WINDOW, GtkApplication
DiAppWindow *di_app_window_new (GtkApplication *app);

/** Set up the window by loading all valid files */
void di_app_window_open (DiAppWindow *win, GFile **files, int n_files);
void di_app_window_open (DiAppWindow *win, gboolean autoclose, GFile **files, int n_files);

/** Exit the application if a drag event ends within the application window. */
void drag_end_cb (void);
void drag_end_cb (GtkDragSource *self, GdkDrag *drag, gboolean delete_data, gpointer user_data);
11 changes: 4 additions & 7 deletions src/di-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
#include "di-app.h"
#include "di-app-window.h"

/** Global Variables */
gboolean autoclose = FALSE; // If true, the application closes after drag-drop operations

struct _DiApp {
GtkApplication parent;
gboolean autoclose; // If true, the application closes after drag-drop operations
};

G_DEFINE_TYPE (DiApp, di_app, GTK_TYPE_APPLICATION)
Expand All @@ -25,9 +23,8 @@ static GActionEntry action_entries[] = {
};

static gint di_app_handle_local_options (GApplication *app, GVariantDict *options) {
if (g_variant_dict_contains (options, "autoclose")) {
autoclose = TRUE;
}
DiApp *di_app = DI_APP (app);
di_app->autoclose = g_variant_dict_contains (options, "autoclose");

// The return value of -1 indicates that the application keeps running
return -1;
Expand Down Expand Up @@ -56,7 +53,7 @@ static void di_app_open (GApplication *app, GFile **files, int n_files, const ch
gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), 0);

win = di_app_window_new (GTK_APPLICATION (app));
di_app_window_open (win, files, n_files);
di_app_window_open (win, DI_APP (app)->autoclose, files, n_files);
gtk_window_present (GTK_WINDOW (win));
}

Expand Down
4 changes: 2 additions & 2 deletions src/di-file-cell.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ DiFileCell *di_file_cell_new (void) {
}

/** Set up the file cell by loading information about the file it represents. */
void di_file_cell_load (DiFileCell *cell, GFile *file) {
void di_file_cell_load (DiFileCell *cell, GFile *file, DiAppWindow *win) {
GFileInfo *fileInfo;
GdkTexture *texture;
GError *err = NULL;
Expand Down Expand Up @@ -87,7 +87,7 @@ void di_file_cell_load (DiFileCell *cell, GFile *file) {
gtk_drag_source_set_content (dsource, contentProvider);
gtk_drag_source_set_icon (dsource, GDK_PAINTABLE (iconPaintable), 0, 0);
g_object_unref (contentProvider);
g_signal_connect (dsource, "drag-end", G_CALLBACK (drag_end_cb), NULL);
g_signal_connect (dsource, "drag-end", G_CALLBACK (drag_end_cb), win);
gtk_widget_add_controller (GTK_WIDGET (cell), GTK_EVENT_CONTROLLER (dsource));
}

Expand Down
3 changes: 2 additions & 1 deletion src/di-file-cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#pragma once

#include <gtk/gtk.h>
#include "di-app-window.h"

#define DI_FILE_CELL_TYPE (di_file_cell_get_type ())
G_DECLARE_FINAL_TYPE (DiFileCell, di_file_cell, DI, FILE_CELL, GtkBox)

DiFileCell *di_file_cell_new (void);

/** Set up the file cell by loading information about the file it represents. */
void di_file_cell_load (DiFileCell *cell, GFile *file);
void di_file_cell_load (DiFileCell *cell, GFile *file, DiAppWindow *win);

0 comments on commit 14570dc

Please sign in to comment.