-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsb_toolbar.c
More file actions
141 lines (122 loc) · 4.53 KB
/
Copy pathsb_toolbar.c
File metadata and controls
141 lines (122 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* ShellBar v1.9.0 — A command-bar terminal emulator built on libghostty-vt
* Copyright (c) 2026 Xavier Araque <xavieraraque@gmail.com>
* MIT License
*/
#include "sb_toolbar.h"
#include <string.h>
struct _SbToolbar {
GtkWidget *widget;
GtkBox *box;
SbTerminal *active_terminal;
GtkWidget **buttons;
int button_count;
SbToolbarCommandCb command_cb;
void *command_cb_data;
};
SbToolbar *sb_toolbar_new(GtkOrientation orientation) {
SbToolbar *self = g_malloc0(sizeof(SbToolbar));
self->widget = gtk_box_new(orientation, 4);
self->box = GTK_BOX(self->widget);
if (orientation == GTK_ORIENTATION_HORIZONTAL) {
gtk_widget_set_margin_start(self->widget, 6);
gtk_widget_set_margin_end(self->widget, 6);
gtk_widget_set_margin_top(self->widget, 4);
gtk_widget_set_margin_bottom(self->widget, 4);
} else {
gtk_widget_set_margin_top(self->widget, 6);
gtk_widget_set_margin_bottom(self->widget, 6);
gtk_widget_set_margin_start(self->widget, 4);
gtk_widget_set_margin_end(self->widget, 4);
}
gtk_widget_add_css_class(self->widget, "sb-toolbar");
return self;
}
static gboolean sb_toolbar_flash_timeout(gpointer user_data) {
GtkWidget **target = user_data;
if (*target) {
g_object_remove_weak_pointer(G_OBJECT(*target), (gpointer *)target);
gtk_widget_remove_css_class(*target, "sb-flash");
}
g_free(target);
return G_SOURCE_REMOVE;
}
static void sb_toolbar_flash_button(GtkWidget *button) {
gtk_widget_add_css_class(button, "sb-flash");
GtkWidget **target = g_new0(GtkWidget *, 1);
*target = button;
g_object_add_weak_pointer(G_OBJECT(button), (gpointer *)target);
g_timeout_add(200, sb_toolbar_flash_timeout, target);
}
static void on_button_clicked(GtkButton *button, gpointer user_data) {
SbToolbar *self = user_data;
const char *cmd = g_object_get_data(G_OBJECT(button), "sb-command");
int index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "sb-index"));
if (cmd && self->active_terminal) {
sb_toolbar_flash_button(GTK_WIDGET(button));
sb_toolbar_set_button_highlight(self, index, TRUE);
if (self->command_cb)
self->command_cb(index, self->command_cb_data);
sb_terminal_write_str(self->active_terminal, cmd);
sb_terminal_write(self->active_terminal, "\n", 1);
}
}
void sb_toolbar_set_button_highlight(SbToolbar *self, int index,
gboolean active) {
if (index < 0 || index >= self->button_count) return;
GtkWidget *btn = self->buttons[index];
if (!btn || !GTK_IS_WIDGET(btn)) return;
if (active)
gtk_widget_add_css_class(btn, "sb-active");
else
gtk_widget_remove_css_class(btn, "sb-active");
}
void sb_toolbar_set_command_callback(SbToolbar *self, SbToolbarCommandCb cb,
void *userdata) {
self->command_cb = cb;
self->command_cb_data = userdata;
}
static GtkWidget *sb_toolbar_add_button(SbToolbar *self, const char *name,
const char *command, const char *icon_name,
int index) {
GtkWidget *button;
if (icon_name && icon_name[0] != '\0')
button = gtk_button_new_from_icon_name(icon_name);
else
button = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(button), name);
gtk_widget_set_tooltip_text(button, command);
g_object_set_data_full(G_OBJECT(button), "sb-command",
g_strdup(command), g_free);
g_object_set_data(G_OBJECT(button), "sb-index",
GINT_TO_POINTER(index));
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), self);
gtk_box_append(self->box, button);
return button;
}
void sb_toolbar_set_buttons(SbToolbar *self, const SbToolbarButtonDef *buttons,
int count) {
GtkWidget *child;
while ((child = gtk_widget_get_first_child(self->widget)) != NULL)
gtk_box_remove(self->box, child);
g_free(self->buttons);
self->buttons = g_malloc0(count * sizeof(GtkWidget *));
self->button_count = count;
for (int i = 0; i < count; i++)
self->buttons[i] = sb_toolbar_add_button(self, buttons[i].name,
buttons[i].command, buttons[i].icon_name, i);
GtkWidget *add_btn = gtk_button_new_with_label("+");
gtk_widget_set_tooltip_text(add_btn, "Add command");
gtk_box_append(self->box, add_btn);
}
void sb_toolbar_set_active_terminal(SbToolbar *self, SbTerminal *terminal) {
self->active_terminal = terminal;
}
GtkWidget *sb_toolbar_get_widget(SbToolbar *self) {
return self->widget;
}
void sb_toolbar_free(SbToolbar *self) {
if (!self) return;
g_free(self->buttons);
g_free(self);
}