-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathwindow.js
More file actions
194 lines (161 loc) · 4.35 KB
/
window.js
File metadata and controls
194 lines (161 loc) · 4.35 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";
import Editor from "./editor.js";
import { settings } from "./util.js";
import { parse, format, isEmptyCommitMessage } from "./scm.js";
import ThemeSelector from "./ThemeSelector.js";
import Interface from "./window.blp";
Gio._promisify(Adw.MessageDialog.prototype, "choose", "choose_finish");
export default function Window({ application, file, text, type, readonly }) {
const builder = Gtk.Builder.new_from_resource(Interface);
let parsed = {};
try {
parsed = parse(text, type);
} catch (err) {
if (__DEV__) {
logError(err);
}
}
const window = builder.get_object("window");
if (__DEV__) window.add_css_class("devel");
settings.bind(
"window-width",
window,
"default-width",
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
"window-height",
window,
"default-height",
Gio.SettingsBindFlags.DEFAULT,
);
let title = GLib.path_get_basename(GLib.get_current_dir());
if (parsed.detail) title += ` (${parsed.detail})`;
window.set_title(title);
// Popover menu theme switcher
const button_menu = builder.get_object("menubutton");
const popover = button_menu.get_popover();
popover.add_child(new ThemeSelector(), "themeswitcher");
const button_save = builder.get_object("button_save");
button_save.label = parsed.action;
// Set a 3px padding on the bottom right floating menu button
{
const margin_box = builder
.get_object("menubutton")
.get_first_child()
.get_first_child();
["top", "end", "start", "bottom"].forEach((dir) => {
margin_box["margin_" + dir] = 10;
});
}
const { buffer, source_view, editor } = Editor({
builder,
button_save,
type,
parsed,
});
let has_changes = false;
buffer.connect("changed", () => {
has_changes = true;
});
window.set_application(application);
const action_cancel = new Gio.SimpleAction({
name: "cancel",
parameter_type: null,
});
action_cancel.connect("activate", () => {
const { text } = buffer;
abort({
type,
file,
value: text.trim(),
readonly,
window,
application,
has_changes,
}).catch(logError);
});
window.add_action(action_cancel);
const action_save = new Gio.SimpleAction({
name: "save",
parameter_type: null,
});
action_save.connect("activate", () => {
const { text } = buffer;
if (isEmptyCommitMessage(text, parsed.comment_prefix)) return;
const value =
parsed.is_message && !editor.isWiderThanWrapWidthRequest()
? format(
text,
settings.get_int("body-length-wrap"),
parsed.comment_prefix,
)
: text;
save({
file,
value,
readonly,
});
application.quit();
});
window.add_action(action_save);
// https://github.com/sonnyp/Commit/issues/33
window.set_focus(source_view);
return { window };
}
function shouldSaveOnAbort({ type }) {
return type !== "config";
}
async function abort({
type,
file,
value,
readonly,
window,
application,
has_changes,
}) {
if (!shouldSaveOnAbort({ type })) {
application.quit();
return;
}
if (await confirmDiscard({ type, value, window, has_changes })) {
save({ file, value: "", readonly });
application.quit();
}
}
function save({ file, value, readonly }) {
if (readonly) return;
try {
GLib.file_set_contents(file.get_path(), value);
} catch (err) {
logError(err);
}
}
function shouldConfirmOnDiscard({ type, value, has_changes }) {
if (!has_changes) return false;
if (type !== "commit") return false;
if (!value) return false;
return true;
}
async function confirmDiscard({ type, value, window, has_changes }) {
if (!shouldConfirmOnDiscard({ type, value, has_changes })) return true;
const cancel = "cancel";
const discard = "discard";
const dialog = new Adw.MessageDialog({
heading: _("Discard changes?"),
close_response: "cancel",
modal: true,
transient_for: window,
});
dialog.add_response(cancel, _("Cancel"));
dialog.add_response(discard, _("Discard"));
dialog.set_response_appearance("discard", Adw.ResponseAppearance.DESTRUCTIVE);
dialog.set_default_response("discard");
dialog.present();
const response = await dialog.choose(null);
return response === discard;
}