nvdialog_rs/about_dialog.rs
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2022-2025 Aggelos Tselios
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25use nvdialog_sys::ffi::*;
26use std::os::raw::c_void;
27
28use crate::{cstr, Image, Object};
29
30/// A struct for a dialog to show about your application.
31///
32/// Dialogs like this are used in the traditional Help > About dialogs found in most programs.
33/// In this case, this `AboutDialog` uses, like the rest of the library, the OS' native toolkit to show
34/// it. This may create inconsistency in some situations, for example in web apps.
35///
36/// # Examples
37/// Basic about dialog:
38/// ```rust
39/// use nvdialog_rs::AboutDialog;
40///
41/// let dialog = AboutDialog::new()
42/// .name("App Name".into())
43/// .description("A short description for your app".into())
44/// .version("0.1.0")
45/// .build();
46///
47/// dialog.show()
48/// ```
49pub struct AboutDialog {
50 app_name: String,
51 details: String,
52 version: String,
53 icon: Option<Image>,
54 raw: *mut NvdAboutDialog,
55}
56
57impl AboutDialog {
58 pub fn new() -> Self {
59 Self {
60 app_name: String::new(),
61 details: String::new(),
62 version: String::new(),
63 icon: None,
64 raw: std::ptr::null_mut(),
65 }
66 }
67
68 pub fn name<S: ToString>(mut self, name: S) -> Self {
69 self.app_name = name.to_string();
70 self
71 }
72
73 pub fn description<S: ToString>(mut self, description: S) -> Self {
74 self.details = description.to_string();
75 self
76 }
77
78 pub fn version<S: ToString>(mut self, version: S) -> Self {
79 self.version = version.to_string();
80 self
81 }
82
83 pub fn icon(mut self, icon: Image) -> Self {
84 self.icon = Some(icon);
85 self
86 }
87
88 pub fn build(mut self) -> Self {
89 let dialog = unsafe {
90 let n = cstr!(&*self.app_name);
91 let d = cstr!(&*self.details);
92 let v = cstr!(&*self.version);
93 let raw = nvd_about_dialog_new(n.as_ptr(), d.as_ptr(), std::ptr::null_mut());
94 nvd_about_dialog_set_version(raw, v.as_ptr());
95 if let Some(ref i) = self.icon {
96 nvd_dialog_set_icon(raw, i.get_raw())
97 }
98 raw
99 };
100
101 self.raw = dialog;
102 self
103 }
104}
105
106impl Object for AboutDialog {
107 type NativeType = NvdAboutDialog;
108 type ReturnValue = ();
109
110 fn get_raw(&self) -> *mut Self::NativeType {
111 self.raw
112 }
113
114 fn show(&self) {
115 unsafe { nvd_show_about_dialog(self.raw) }
116 }
117
118 fn free(&mut self) {
119 unsafe { nvd_free_object(self.raw as *mut c_void) }
120 }
121}
122
123impl Drop for AboutDialog {
124 fn drop(&mut self) {
125 self.free();
126 }
127}