Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
Improve support for AdwComboRow
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Jan 3, 2024
1 parent e0310ca commit ab9bed9
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
1 change: 1 addition & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
- [banner_on_click_cb(ptr_userData_)](methods/banner_on_click_cb(ptr_userData_).md)
- [button_on_click_cb(ptr_userData_)](methods/button_on_click_cb(ptr_userData_).md)
- [checkbutton_on_toggle_cb(ptr_userData_)](methods/checkbutton_on_toggle_cb(ptr_userData_).md)
- [comborow_on_change_cb(ptr_parameter_userData_)](methods/comborow_on_change_cb(ptr_parameter_userData_).md)
- [entryrow_on_submit_cb(ptr_userData_)](methods/entryrow_on_submit_cb(ptr_userData_).md)
- [entryrow_on_update_cb(ptr_userData_)](methods/entryrow_on_update_cb(ptr_userData_).md)
- [filedialog_on_open_cb(ptr_file_userData_)](methods/filedialog_on_open_cb(ptr_file_userData_).md)
Expand Down
32 changes: 31 additions & 1 deletion Documentation/Reference/classes/ComboRow.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ Adw.ComboRow

The identifier of the string list.

### `strings`

The content.

### `handler`

The handler gets called when the selection changes.

## Methods
### `init(title:subtitle:)`

Expand All @@ -23,7 +31,29 @@ Append a string.
- Parameter string: The string.
- Returns: The combo row.

### `remove(at:)`

Remove a string.
- Parameter index: The index.
- Returns: The combo row.

### `getContent()`

Get the string content.
- Returns: The content.

### `selected()`

Get the selected row.
- Returns: The string of the selected row..
- Returns: The string of the selected row.

### `select(at:)`

Set the selected row.
- Parameter index: The index of the new selection.

### `onChange(handler:)`

Set a handler for when the selection changes.
- Parameter handler: The handlers.
- Returns: The combo row.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### `comborow_on_change_cb(ptr:parameter:userData:)`

Handle when the combo row's selection changes.
- Parameters:
- ptr: The pointer.
- parameter: The additional data.
- userData: The combo row data.
41 changes: 41 additions & 0 deletions Sources/CGTUI/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ button_on_click_cb (void *, void *);
static void
checkbutton_on_toggle_cb (void *, void *);
static void
comborow_on_change_cb (void *, void *, void *);
static void
entryrow_on_submit_cb (void *, void *);
static void
entryrow_on_update_cb (void *, void *);
Expand Down Expand Up @@ -971,6 +973,37 @@ gtui_create_comborow (uint64_t list)
return row;
}

static void
gtui_comborow_init_signals (uint64_t cr, uint64_t data)
{
AdwComboRow *comborow;

g_assert_nonnull (cr);
g_assert_nonnull (data);
g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)cr)));

comborow = ADW_COMBO_ROW (cr);
swift_retain (data);
g_signal_connect (comborow, "notify::selected", G_CALLBACK (comborow_on_change_cb), (void *)data);
}

static char *
gtui_comborow_get_selected (uint64_t row, uint64_t list)
{
g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)row)));
g_assert (GTK_IS_STRING_LIST (GTK_STRING_LIST ((void *)list)));

return gtk_string_list_get_string (list, adw_combo_row_get_selected (row));
}

static void
gtui_comborow_set_selected (uint64_t row, int position)
{
g_assert (ADW_IS_COMBO_ROW (ADW_COMBO_ROW ((void *)row)));

adw_combo_row_set_selected (row, position);
}

static uint64_t
gtui_create_expanderrow ()
{
Expand Down Expand Up @@ -2139,6 +2172,14 @@ gtui_stringlist_append (uint64_t list, const char *str)
gtk_string_list_take (list, strdup (str));
}

static void
gtui_stringlist_remove (uint64_t list, int position)
{
g_assert (GTK_IS_STRING_LIST (list));

gtk_string_list_remove (list, position);
}

static void
gtui_box_remove (uint64_t box, uint64_t widget)
{
Expand Down
56 changes: 54 additions & 2 deletions Sources/Libadwaita/Adwaita/ComboRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class ComboRow: ActionRow {

/// The identifier of the string list.
private let stringListPtr: UInt64
/// The content.
private var strings: [String] = []
/// The handler gets called when the selection changes.
var handler: () -> Void = { }

/// Initialize a combo row.
/// - Parameters:
Expand All @@ -21,6 +25,8 @@ public class ComboRow: ActionRow {
self.stringListPtr = gtui_create_stringlist()
super.init()
self.nativePtr = gtui_create_comborow(self.stringListPtr)
let selfAddr = unsafeBitCast(self, to: UInt64.self)
gtui_comborow_init_signals(self.nativePtr, selfAddr)
_ = self.title(title)
_ = self.subtitle(subtitle)
}
Expand All @@ -30,17 +36,63 @@ public class ComboRow: ActionRow {
/// - Returns: The combo row.
public func append(_ string: String) -> ComboRow {
gtui_stringlist_append(self.stringListPtr, string.cString)
strings.append(string)
return self
}

/// Remove a string.
/// - Parameter index: The index.
/// - Returns: The combo row.
public func remove(at index: Int) -> ComboRow {
gtui_stringlist_remove(self.stringListPtr, index.cInt)
strings.remove(at: index)
return self
}

/// Get the string content.
/// - Returns: The content.
public func getContent() -> [String] {
strings
}

/// Get the selected row.
/// - Returns: The string of the selected row..
/// - Returns: The string of the selected row.
public func selected() -> String {
let str = gtui_stringlist_get_selected(self.nativePtr, self.stringListPtr)
let str = gtui_comborow_get_selected(self.nativePtr, self.stringListPtr)
if let str {
return String(cString: str)
} else {
return ""
}
}

/// Set the selected row.
/// - Parameter index: The index of the new selection.
public func select(at index: Int) {
gtui_comborow_set_selected(self.nativePtr, index.cInt)
}

/// Set a handler for when the selection changes.
/// - Parameter handler: The handlers.
/// - Returns: The combo row.
public func onChange(handler: @escaping () -> Void) -> ComboRow {
self.handler = handler
return self
}

}

/// Handle when the combo row's selection changes.
/// - Parameters:
/// - ptr: The pointer.
/// - parameter: The additional data.
/// - userData: The combo row data.
@_cdecl("comborow_on_change_cb")
func comborow_on_change_cb(
ptr: UnsafeMutableRawPointer,
parameter: UnsafeMutableRawPointer,
userData: UnsafeMutableRawPointer
) {
let row = Unmanaged<ComboRow>.fromOpaque(userData).takeUnretainedValue()
row.handler()
}
5 changes: 4 additions & 1 deletion Tests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,16 @@ public class MyApplication: Application {
let stack = Stack().append(content1, transition: .slideUp)
.append(content2, transition: .slideUp)
preferencesWindow.setDefaultSize(width: 500, height: 400)
let combo = ComboRow(title: "ComboRow", subtitle: "Description")
preferencesWindow.add(
page: .init(name: "Hello", icon: .default(icon: .daytimeSunrise), description: "Hello world?")
.add(
group: group
.add(ActionRow(title: "ActionRow", subtitle: "Description").addSuffix(stack))
.add(
ComboRow(title: "ComboRow", subtitle: "Description").append("Hello").append("World")
combo.append("Hello").append("World").onChange {
print(combo.selected())
}
)
.headerSuffix(
Button("Add")
Expand Down

0 comments on commit ab9bed9

Please sign in to comment.