kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::path::Path;

use crate::MenuItem;

/// Set the dock badge count using the Unity Launcher API via D-Bus.
///
/// Uses the `com.canonical.Unity.LauncherEntry` interface to set the
/// `count` and `count-visible` properties on the application's launcher entry.
/// This is supported by Unity, Plank, and some other Linux docks.
pub fn set_dock_badge(label: Option<&str>) {
    let desktop_uri = match get_desktop_file_uri() {
        Some(uri) => uri,
        None => return,
    };

    match label {
        Some(text) if !text.is_empty() => {
            // Try to parse the label as a number for the count property.
            // Unity Launcher API uses an integer count, not arbitrary text.
            let count: i64 = text.parse().unwrap_or(0);
            let _ = std::process::Command::new("dbus-send")
                .args([
                    "--session",
                    "--dest=com.canonical.Unity",
                    "--type=method_call",
                    "/com/canonical/Unity/LauncherEntry",
                    "com.canonical.Unity.LauncherEntry.Update",
                    &format!("string:{}", desktop_uri),
                    &format!(
                        "dict:string:variant:count,int64:{},count-visible,boolean:true",
                        count
                    ),
                ])
                .output();
        }
        _ => {
            // Clear the badge by setting count-visible to false
            let _ = std::process::Command::new("dbus-send")
                .args([
                    "--session",
                    "--dest=com.canonical.Unity",
                    "--type=method_call",
                    "/com/canonical/Unity/LauncherEntry",
                    "com.canonical.Unity.LauncherEntry.Update",
                    &format!("string:{}", desktop_uri),
                    "dict:string:variant:count-visible,boolean:false",
                ])
                .output();
        }
    }
}

/// Set the dock menu by exposing menu items via the `com.canonical.dbusmenu`
/// D-Bus interface.
///
/// This writes a simple menu description that compatible docks can pick up.
/// Since the full dbusmenu protocol requires a persistent D-Bus service,
/// this implementation uses `dbus-send` to signal menu updates.
/// In practice, many Linux docks read the desktop actions from the .desktop file,
/// so we also update the .desktop file with the menu actions as a fallback.
pub fn set_dock_menu(menu: &[MenuItem], _keymap: &crate::Keymap) {
    // Extract action names from menu items and write them to the .desktop file
    // as desktop actions, which is the most widely supported approach on Linux.
    let actions = extract_action_names(menu);
    if actions.is_empty() {
        return;
    }

    let Some(desktop_file_path) = find_desktop_file_path() else {
        return;
    };

    // Read existing .desktop file content
    let existing_content = std::fs::read_to_string(&desktop_file_path).unwrap_or_default();

    // Build the updated .desktop file with Actions
    let action_ids: Vec<String> = actions
        .iter()
        .enumerate()
        .map(|(i, _)| format!("DockAction{}", i))
        .collect();

    let actions_line = format!("Actions={};", action_ids.join(";"));

    // Rebuild the [Desktop Entry] section, replacing or adding the Actions line
    let mut new_content = String::new();
    let mut found_actions = false;
    let mut in_desktop_entry = false;
    let mut past_desktop_entry = false;

    for line in existing_content.lines() {
        if line.starts_with("[Desktop Entry]") {
            in_desktop_entry = true;
            past_desktop_entry = false;
            new_content.push_str(line);
            new_content.push('\n');
            continue;
        }
        if line.starts_with('[') && in_desktop_entry {
            // We've left the [Desktop Entry] section
            in_desktop_entry = false;
            past_desktop_entry = true;
        }
        if in_desktop_entry && line.starts_with("Actions=") {
            new_content.push_str(&actions_line);
            new_content.push('\n');
            found_actions = true;
            continue;
        }
        // Skip old [Desktop Action ...] sections that we generated
        if line.starts_with("[Desktop Action DockAction") {
            // Skip this section entirely
            past_desktop_entry = true;
            continue;
        }
        if past_desktop_entry && !line.starts_with('[') && !line.is_empty() {
            // Still in a skipped action section
            continue;
        }
        if line.starts_with('[') {
            past_desktop_entry = false;
        }
        new_content.push_str(line);
        new_content.push('\n');
    }

    if !found_actions && !new_content.is_empty() {
        // Append Actions line at the end of the content
        new_content.push_str(&actions_line);
        new_content.push('\n');
    }

    // Append the action sections
    for (i, name) in actions.iter().enumerate() {
        new_content.push_str(&format!(
            "\n[Desktop Action DockAction{}]\nName={}\n",
            i, name
        ));
    }

    let _ = std::fs::write(&desktop_file_path, new_content);
}

/// Add a file path to the recently-used documents list.
///
/// Writes to `~/.local/share/recently-used.xbel` in the XBEL bookmark format,
/// which is the standard location for recent documents on freedesktop-compliant
/// Linux desktops.
pub fn add_recent_document(path: &Path) {
    let path_str = match path.to_str() {
        Some(s) => s,
        None => return,
    };

    // Convert the file path to a file:// URI
    let uri = format!("file://{}", path_str);

    let mime_type = guess_mime_type(path);
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let app_name = std::env::current_exe()
        .ok()
        .and_then(|p| p.file_stem().map(|s| s.to_string_lossy().into_owned()))
        .unwrap_or_else(|| "gpui-app".to_string());

    let xbel_path = get_recently_used_xbel_path();
    let Some(xbel_path) = xbel_path else {
        return;
    };

    // Read existing content or create new file
    let existing = std::fs::read_to_string(&xbel_path).unwrap_or_default();

    let bookmark_entry = format!(
        r#"  <bookmark href="{uri}" modified="{timestamp}" visited="{timestamp}">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="{mime}"/>
        <bookmark:applications>
          <bookmark:application name="{app}" exec="&apos;{app}&apos; %u" modified="{timestamp}" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>"#,
        uri = escape_xml(&uri),
        timestamp = format_iso8601(timestamp),
        mime = escape_xml(&mime_type),
        app = escape_xml(&app_name),
    );

    if existing.contains("<xbel") {
        // Update existing file: remove any existing bookmark for this URI, then add new one
        let mut new_content = String::new();
        let mut skip_bookmark = false;

        for line in existing.lines() {
            if line.contains(&format!("href=\"{}\"", escape_xml(&uri))) {
                skip_bookmark = true;
                continue;
            }
            if skip_bookmark {
                if line.contains("</bookmark>") {
                    skip_bookmark = false;
                }
                continue;
            }
            if line.contains("</xbel>") {
                new_content.push_str(&bookmark_entry);
                new_content.push('\n');
            }
            new_content.push_str(line);
            new_content.push('\n');
        }

        let _ = std::fs::write(&xbel_path, new_content);
    } else {
        // Create new XBEL file
        let content = format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<xbel version="1.0"
      xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"
      xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info">
{bookmark}
</xbel>
"#,
            bookmark = bookmark_entry,
        );

        // Ensure parent directory exists
        if let Some(parent) = xbel_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        let _ = std::fs::write(&xbel_path, content);
    }
}

/// Get the desktop file URI for the current application.
/// Returns something like "application://myapp.desktop"
fn get_desktop_file_uri() -> Option<String> {
    let app_name = std::env::current_exe()
        .ok()?
        .file_stem()?
        .to_string_lossy()
        .into_owned();

    Some(format!("application://{}.desktop", app_name))
}

/// Find the .desktop file path for the current application.
fn find_desktop_file_path() -> Option<std::path::PathBuf> {
    let app_name = std::env::current_exe()
        .ok()?
        .file_stem()?
        .to_string_lossy()
        .into_owned();

    let data_dir = std::env::var_os("XDG_DATA_HOME")
        .map(std::path::PathBuf::from)
        .or_else(|| {
            std::env::var_os("HOME")
                .map(|home| std::path::PathBuf::from(home).join(".local").join("share"))
        })?;

    let desktop_file = data_dir
        .join("applications")
        .join(format!("{}.desktop", app_name));

    if desktop_file.exists() {
        return Some(desktop_file);
    }

    // Also check the url-handler variant
    let url_handler_file = data_dir
        .join("applications")
        .join(format!("{}-url-handler.desktop", app_name));

    if url_handler_file.exists() {
        return Some(url_handler_file);
    }

    // Check system-wide locations
    for dir in &["/usr/share/applications", "/usr/local/share/applications"] {
        let path = std::path::Path::new(dir).join(format!("{}.desktop", app_name));
        if path.exists() {
            return Some(path);
        }
    }

    None
}

/// Extract action names from MenuItem list (only Action variants).
fn extract_action_names(items: &[MenuItem]) -> Vec<String> {
    let mut names = Vec::new();
    for item in items {
        match item {
            MenuItem::Action { name, .. } => {
                names.push(name.to_string());
            }
            MenuItem::Submenu(menu) => {
                // Flatten submenu items
                names.extend(extract_action_names(&menu.items));
            }
            _ => {}
        }
    }
    names
}

/// Get the path to the recently-used.xbel file.
fn get_recently_used_xbel_path() -> Option<std::path::PathBuf> {
    let data_dir = std::env::var_os("XDG_DATA_HOME")
        .map(std::path::PathBuf::from)
        .or_else(|| {
            std::env::var_os("HOME")
                .map(|home| std::path::PathBuf::from(home).join(".local").join("share"))
        })?;

    Some(data_dir.join("recently-used.xbel"))
}

/// Guess the MIME type from a file extension.
fn guess_mime_type(path: &Path) -> String {
    match path.extension().and_then(|e| e.to_str()) {
        Some("txt") => "text/plain".to_string(),
        Some("rs") => "text/x-rust".to_string(),
        Some("py") => "text/x-python".to_string(),
        Some("js") => "application/javascript".to_string(),
        Some("ts") => "application/typescript".to_string(),
        Some("html") | Some("htm") => "text/html".to_string(),
        Some("css") => "text/css".to_string(),
        Some("json") => "application/json".to_string(),
        Some("xml") => "application/xml".to_string(),
        Some("pdf") => "application/pdf".to_string(),
        Some("png") => "image/png".to_string(),
        Some("jpg") | Some("jpeg") => "image/jpeg".to_string(),
        Some("gif") => "image/gif".to_string(),
        Some("svg") => "image/svg+xml".to_string(),
        Some("md") => "text/markdown".to_string(),
        Some("toml") => "application/toml".to_string(),
        Some("yaml") | Some("yml") => "application/yaml".to_string(),
        Some("c") => "text/x-c".to_string(),
        Some("cpp") | Some("cc") | Some("cxx") => "text/x-c++".to_string(),
        Some("h") | Some("hpp") => "text/x-c".to_string(),
        Some("java") => "text/x-java".to_string(),
        Some("go") => "text/x-go".to_string(),
        Some("rb") => "text/x-ruby".to_string(),
        Some("sh") => "application/x-shellscript".to_string(),
        Some("zip") => "application/zip".to_string(),
        Some("tar") => "application/x-tar".to_string(),
        Some("gz") => "application/gzip".to_string(),
        _ => "application/octet-stream".to_string(),
    }
}

/// Escape special XML characters.
fn escape_xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Format a Unix timestamp as ISO 8601 date-time string.
fn format_iso8601(timestamp: u64) -> String {
    // Simple ISO 8601 formatting without external dependencies
    let secs_per_day: u64 = 86400;
    let secs_per_hour: u64 = 3600;
    let secs_per_minute: u64 = 60;

    let days = timestamp / secs_per_day;
    let remaining = timestamp % secs_per_day;
    let hours = remaining / secs_per_hour;
    let remaining = remaining % secs_per_hour;
    let minutes = remaining / secs_per_minute;
    let seconds = remaining % secs_per_minute;

    // Calculate year, month, day from days since epoch (1970-01-01)
    let (year, month, day) = days_to_date(days);

    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

/// Convert days since Unix epoch to (year, month, day).
fn days_to_date(days: u64) -> (u64, u64, u64) {
    // Algorithm from http://howardhinnant.github.io/date_algorithms.html
    let z = days + 719468;
    let era = z / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}