Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Library/demos/Account/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg libportal-gtk4

private Gtk.Revealer revealer;
private Adw.EntryRow entry;
private Adw.Avatar avatar;
private Gtk.Label username;
private Gtk.Label display;
private Xdp.Portal portal;
private Xdp.Parent parent;

public void main () {
portal = new Xdp.Portal ();
parent = Xdp.parent_new_gtk (workbench.window);

revealer = (Gtk.Revealer) workbench.builder.get_object ("revealer");
entry = (Adw.EntryRow) workbench.builder.get_object ("entry");
avatar = (Adw.Avatar) workbench.builder.get_object ("avatar");
username = (Gtk.Label) workbench.builder.get_object ("username");
display = (Gtk.Label) workbench.builder.get_object ("name");

var button = (Gtk.Button) workbench.builder.get_object ("button");
button.clicked.connect (on_button_clicked);
}

private async void on_button_clicked () {
try {
string reason = entry.text;
Variant result = yield portal.get_user_information (parent, reason, NONE, null);

/*
* result is a Variant dictionary containing the following fields:
* id (s): the user id
* name (s): the users real name
* image (s): the uri of an image file for the users avatar picture
*/

var id = (string) result.lookup_value ("id", VariantType.STRING);
var name = (string) result.lookup_value ("name", VariantType.STRING);
var uri = (string) result.lookup_value ("image", VariantType.STRING);

var file = File.new_for_uri (uri);
var texture = Gdk.Texture.from_file (file);

username.label = id;
display.label = name;
avatar.custom_image = texture;
revealer.reveal_child = true;

entry.text = "";
message ("Information Retrieved");
} catch (Error e) {
critical (e.message);
}
}
36 changes: 36 additions & 0 deletions src/Library/demos/Color Picker/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg libportal-gtk4

private Xdp.Portal portal;
private Xdp.Parent parent;

public void main () {
portal = new Xdp.Portal ();
parent = Xdp.parent_new_gtk (workbench.window);

var button = (Gtk.Button) workbench.builder.get_object ("button");
button.clicked.connect (on_button_clicked);
}

private async void on_button_clicked () {
try {
// result is a variant of the form (ddd), containing red green and blue components in the range [0,1]
Variant result = yield portal.pick_color (parent, null);

double r, g, b;
VariantIter iter = result.iterator (); // Iterate over the array in the variant
iter.next ("d", out r);
iter.next ("d", out g);
iter.next ("d", out b);

var color = Gdk.RGBA () {
red = (float) r,
green = (float) g,
blue = (float) b,
alpha = 1.0f
};

message (@"Selected color is $color");
} catch (Error e) {
critical (e.message);
}
}
40 changes: 40 additions & 0 deletions src/Library/demos/Email/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg libportal-gtk4

private Xdp.Portal portal;
private Xdp.Parent parent;
private Gtk.Entry entry;

public void main () {
portal = new Xdp.Portal ();
parent = Xdp.parent_new_gtk (workbench.window);

entry = (Gtk.Entry) workbench.builder.get_object ("entry");
var button = (Gtk.Button) workbench.builder.get_object ("button");
button.clicked.connect (on_button_clicked);
}

private async void on_button_clicked () {
string email_address = entry.text;

try {
bool success = yield portal.compose_email (
parent,
{ email_address }, // addresses
null, // cc
null, // bcc
"Email from Workbench", // subject
"Hello World!", // body
null,
NONE,
null
);

if (success) {
message ("Success");
return;
}
message ("Failure: verify that you have an email application.");
} catch (Error e) {
critical (e.message);
}
}
90 changes: 90 additions & 0 deletions src/Library/demos/Event Controllers/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1

private bool ctrl_pressed = false;

public void main () {
Gtk.Window window = workbench.window;

var pic1 = (Gtk.Picture) workbench.builder.get_object ("pic1");
var pic2 = (Gtk.Picture) workbench.builder.get_object ("pic2");

pic1.file = File.new_for_uri (workbench.resolve ("image1.png"));
pic2.file = File.new_for_uri (workbench.resolve ("image2.png"));

var stack = (Gtk.Stack) workbench.builder.get_object ("stack");
var primary_button = (Gtk.Button) workbench.builder.get_object ("primary_button");
var middle_button = (Gtk.Button) workbench.builder.get_object ("middle_button");
var secondary_button = (Gtk.Button) workbench.builder.get_object ("secondary_button");
var ctrl_button = (Gtk.Button) workbench.builder.get_object ("ctrl_button");

var key_controller = new Gtk.EventControllerKey ();
// Gtk.Window hides Gtk.Widget's add_controller method, thus we need to cast it
((Gtk.Widget) window).add_controller (key_controller);

key_controller.key_pressed.connect ((keyval, keycode, state) => {
if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) {
ctrl_pressed = true;
}
return true;
});

key_controller.key_released.connect ((keyval, keycode, state) => {
if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) {
ctrl_pressed = false;
}
});

ctrl_button.clicked.connect (() => {
if (ctrl_pressed) {
ctrl_button.label = "Click to Deactivate";
ctrl_button.add_css_class ("suggested-action");
} else {
ctrl_button.label = "Ctrl + Click to Activate";
ctrl_button.remove_css_class ("suggested-action");
}
});

var gesture_click = new Gtk.GestureClick () {
button = 0
};
((Gtk.Widget) window).add_controller (gesture_click);

gesture_click.pressed.connect ((gesture, n_press, x, y) => {
switch (gesture.get_current_button ()) {
case Gdk.BUTTON_PRIMARY:
primary_button.add_css_class ("suggested-action");
break;
case Gdk.BUTTON_MIDDLE:
middle_button.add_css_class ("suggested-action");
break;
case Gdk.BUTTON_SECONDARY:
secondary_button.add_css_class ("suggested-action");
break;
}
});

gesture_click.released.connect ((gesture, n_press, x, y) => {
switch (gesture.get_current_button ()) {
case Gdk.BUTTON_PRIMARY:
primary_button.remove_css_class ("suggested-action");
break;
case Gdk.BUTTON_MIDDLE:
middle_button.remove_css_class ("suggested-action");
break;
case Gdk.BUTTON_SECONDARY:
secondary_button.remove_css_class ("suggested-action");
break;
}
});

var gesture_swipe = new Gtk.GestureSwipe ();
stack.add_controller (gesture_swipe);

gesture_swipe.swipe.connect ((vel_x, vel_y) => {
if (vel_x > 0) {
stack.visible_child_name = "pic1";
} else {
stack.visible_child_name = "pic2";
}
});
}
125 changes: 125 additions & 0 deletions src/Library/demos/Location/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg libportal-gtk4

private Xdp.Portal portal;
private Xdp.Parent parent;

private Gtk.Revealer revealer;
private Gtk.Button start_button;
private Gtk.Button close_button;
private Gtk.SpinButton distance_threshold;
private Gtk.SpinButton time_threshold;
private Adw.ComboRow accuracy_button;

private Gtk.Label latitude_label;
private Gtk.Label longitude_label;
private Gtk.Label accuracy_label;
private Gtk.Label altitude_label;
private Gtk.Label speed_label;
private Gtk.Label heading_label;
private Gtk.Label description_label;
private Gtk.Label timestamp_label;

public void main () {
portal = new Xdp.Portal ();
parent = Xdp.parent_new_gtk (workbench.window);

revealer = (Gtk.Revealer) workbench.builder.get_object ("revealer");
start_button = (Gtk.Button) workbench.builder.get_object ("start");
close_button = (Gtk.Button) workbench.builder.get_object ("close");
distance_threshold = (Gtk.SpinButton) workbench.builder.get_object ("distance_threshold");
time_threshold = (Gtk.SpinButton) workbench.builder.get_object ("time_threshold");
accuracy_button = (Adw.ComboRow) workbench.builder.get_object ("accuracy_button");

latitude_label = (Gtk.Label) workbench.builder.get_object ("latitude");
longitude_label = (Gtk.Label) workbench.builder.get_object ("longitude");
accuracy_label = (Gtk.Label) workbench.builder.get_object ("accuracy");
altitude_label = (Gtk.Label) workbench.builder.get_object ("altitude");
speed_label = (Gtk.Label) workbench.builder.get_object ("speed");
heading_label = (Gtk.Label) workbench.builder.get_object ("heading");
description_label = (Gtk.Label) workbench.builder.get_object ("description");
timestamp_label = (Gtk.Label) workbench.builder.get_object ("timestamp");

start_button.clicked.connect (start_session);
close_button.clicked.connect (close_session);

time_threshold.value_changed.connect (() => {
message ("Time threshold changed");
restart_session ();
});

distance_threshold.value_changed.connect (() => {
message ("Distance threshold changed");
restart_session ();
});

accuracy_button.notify["selected-item"].connect (() => {
message ("Accuracy changed");
restart_session ();
});

portal.location_updated.connect (on_location_updated);
}

private void on_location_updated (
double latitude,
double longitude,
double altitude,
double accuracy,
double speed,
double heading,
string description,
int64 timestamp_seconds,
int64 timestamp_ms
) {
message ("Location updated");
latitude_label.label = latitude.to_string ();
longitude_label.label = longitude.to_string ();
accuracy_label.label = accuracy.to_string ();
altitude_label.label = altitude.to_string ();
speed_label.label = speed.to_string ();
heading_label.label = heading.to_string ();
description_label.label = description;

// Convert UNIX timestamp to local date and time string
var timestamp = new DateTime.from_unix_local (timestamp_ms);
timestamp_label.label = timestamp.to_string ();
}

private void restart_session () {
portal.location_monitor_stop ();
revealer.reveal_child = false;
start_session.begin ();
}

private async void start_session () {
start_button.sensitive = false;
close_button.sensitive = true;

try {
bool result = yield portal.location_monitor_start (
parent,
(uint) distance_threshold.value,
(uint) time_threshold.value,
(Xdp.LocationAccuracy) accuracy_button.selected,
NONE,
null
);

if (result) {
message ("Location access granted");
revealer.reveal_child = true;
return;
}
message ("Error retrieving location");
} catch (Error e) {
critical (e.message);
}
}

private void close_session () {
start_button.sensitive = false;
close_button.sensitive = false;
portal.location_monitor_stop ();
revealer.reveal_child = false;
message ("Session Closed");
}
Loading