Commit 345cb8e3 authored by Julian Sparber's avatar Julian Sparber

accountsettings: add buttons to address entries

* Add delete and add button to entries
* Create at least one empty entry for email and phone

https://gitlab.gnome.org/World/fractal/issues/21
parent 04ed27fa
......@@ -136,51 +136,10 @@
</packing>
</child>
<child>
<object class="GtkBox" id="account_settings_box_email">
<object class="GtkBox" id="account_settings_email">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkEntry" id="account_settings_email">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
</object>
</child>
<child>
<object class="GtkButton" id="account_settings_email_add">
<property name="visible">False</property>
<property name="can_focus">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
</child>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
<child>
<object class="GtkButton" id="account_settings_email_remove">
<property name="visible">False</property>
<property name="can_focus">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">user-trash-symbolic</property>
</object>
</child>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
<style>
<class name="linked"/>
</style>
<property name="hexpand">True</property>
</object>
<packing>
<property name="top-attach">1</property>
......@@ -204,51 +163,10 @@
</packing>
</child>
<child>
<object class="GtkBox" id="account_settings_box_phone">
<object class="GtkBox" id="account_settings_phone">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkEntry" id="account_settings_phone">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
</object>
</child>
<child>
<object class="GtkButton" id="account_settings_phone_add">
<property name="visible">False</property>
<property name="can_focus">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
</child>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
<child>
<object class="GtkButton" id="account_settings_phone_remove">
<property name="visible">False</property>
<property name="can_focus">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">user-trash-symbolic</property>
</object>
</child>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
<style>
<class name="linked"/>
</style>
<property name="hexpand">True</property>
</object>
<packing>
<property name="top-attach">2</property>
......
......@@ -73,18 +73,11 @@ impl AppOp {
.get_object::<gtk::Grid>("account_settings_grid")
.expect("Can't find account_settings_grid in ui file.");
let email = self.ui.builder
.get_object::<gtk::Box>("account_settings_box_email")
.get_object::<gtk::Box>("account_settings_email")
.expect("Can't find account_settings_box_email in ui file.");
let phone = self.ui.builder
.get_object::<gtk::Box>("account_settings_box_phone")
.get_object::<gtk::Box>("account_settings_phone")
.expect("Can't find account_settings_box_phone in ui file.");
let email_entry = self.ui.builder
.get_object::<gtk::Entry>("account_settings_email")
.expect("Can't find account_settings_email in ui file.");
let phone_entry = self.ui.builder
.get_object::<gtk::Entry>("account_settings_phone")
.expect("Can't find account_settings_phone in ui file.");
let stack = self.ui.builder
.get_object::<gtk::Stack>("account_settings_stack")
.expect("Can't find account_settings_delete_box in ui file.");
......@@ -100,47 +93,51 @@ impl AppOp {
grid.remove_row(i);
}
else {
for w in email.get_children().iter() {
email.remove(w);
}
for w in phone.get_children().iter() {
phone.remove(w);
}
i = i + 1;
}
}
child = grid.get_child_at(1, i);
}
/* Make sure we have at least one empty entry for email and phone */
let empty_email = widgets::Address::new(widgets::AddressType::Email);
let empty_phone = widgets::Address::new(widgets::AddressType::Phone);
email.pack_start(&empty_email.clone().create(None), true, true, 0);
phone.pack_start(&empty_phone.clone().create(None), true, true, 0);
if let Some(data) = data {
for item in data {
if item.medium == "email" {
if first_email {
email_entry.set_text(&item.address);
let entry = gtk::Entry::new();
entry.show();
empty_email.clone().update(Some(item.address));
let entry = widgets::Address::new(widgets::AddressType::Email).create(None);
grid.insert_next_to(&email, gtk::PositionType::Bottom);
grid.attach_next_to(&entry, &email, gtk::PositionType::Bottom, 1, 1);
first_email = false;
}
else {
let entry = gtk::Entry::new();
entry.set_text(&item.address);
entry.show();
let entry = widgets::Address::new(widgets::AddressType::Email).create(Some(item.address));
grid.insert_next_to(&email, gtk::PositionType::Bottom);
grid.attach_next_to(&entry, &email, gtk::PositionType::Bottom, 1, 1);
}
}
}
else if item.medium == "msisdn" {
if first_phone {
let s = String::from("+") + &String::from(item.address);
phone_entry.set_text(&s);
let entry = gtk::Entry::new();
entry.show();
let s = String::from("+") + &String::from(item.address);
empty_phone.clone().update(Some(s));
let entry = widgets::Address::new(widgets::AddressType::Phone).create(None);
grid.insert_next_to(&phone, gtk::PositionType::Bottom);
grid.attach_next_to(&entry, &phone, gtk::PositionType::Bottom, 1, 1);
first_phone = false;
}
else {
let entry = gtk::Entry::new();
let s = String::from("+") + &String::from(item.address);
entry.set_text(&s);
entry.show();
let entry = widgets::Address::new(widgets::AddressType::Phone).create(Some(s));
grid.insert_next_to(&phone, gtk::PositionType::Bottom);
grid.attach_next_to(&entry, &phone, gtk::PositionType::Bottom, 1, 1);
}
......
extern crate gtk;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::cell::RefCell;
use std::rc::Rc;
use self::gtk::prelude::*;
use widgets;
#[derive(Debug, Clone)]
pub enum AddressType {
Email,
Phone,
}
#[derive(Debug, Clone)]
pub enum AddressAction {
Delete,
Add,
}
#[derive(Debug, Clone)]
pub struct Address {
entry: gtk::Entry,
button: gtk::Button,
action: Option<AddressAction>,
medium: AddressType,
}
impl Address {
pub fn new(t: AddressType) -> Address {
let entry = gtk::Entry::new();
let button = gtk::Button::new();
Address {
entry: entry,
button: button,
action: None,
medium: t,
}
}
pub fn create(mut self, text: Option<String>) -> gtk::Box {
let b = gtk::Box::new(gtk::Orientation::Horizontal, 0);
b.pack_start(&self.entry, true, true, 0);
b.pack_end(&self.button, false, false, 0);
if let Some(text) = text {
self.entry.set_text(&text);
self.action = Some(AddressAction::Delete);
let label = gtk::Image::new_from_icon_name("user-trash-symbolic", 1);
self.button.set_image(&label);
self.button.show();
}
else {
self.action = Some(AddressAction::Add);
let label = gtk::Image::new_from_icon_name("list-add-symbolic", 1);
self.button.set_image(&label);
if let Some(style) = self.button.get_style_context() {
style.add_class("suggested-action");
}
self.button.hide();
}
if let Some(style) = b.get_style_context() {
style.add_class("linked");
}
self.entry.show();
self.connect();
b.show();
b
}
pub fn update(mut self, text: Option<String>) {
if let Some(text) = text {
self.entry.set_text(&text);
self.action = Some(AddressAction::Delete);
let label = gtk::Image::new_from_icon_name("user-trash-symbolic", 1);
self.button.set_image(&label);
if let Some(style) = self.button.get_style_context() {
style.remove_class("suggested-action");
}
self.button.show();
}
else {
self.action = Some(AddressAction::Add);
let label = gtk::Image::new_from_icon_name("list-add-symbolic", 1);
self.button.set_image(&label);
if let Some(style) = self.button.get_style_context() {
style.add_class("suggested-action");
}
self.button.hide();
}
}
fn connect(self) {
let button = self.button.clone();
let medium = self.medium.clone();
self.entry.connect_property_text_notify(move |w| {
if let Some(text) = w.get_text() {
if text != "" {
/* FIXME: use better validation */
match medium {
AddressType::Email => button.set_sensitive(text.contains("@") && text.contains(".")),
AddressType::Phone => {},
};
button.show();
}
else {
button.hide();
}
}
});
let button = self.button.clone();
self.entry.connect_activate(move |_| {
let _ = button.emit("clicked", &[]);
});
let medium = self.medium.clone();
let action = self.action.clone();
self.button.connect_clicked(move |w| {
if w.get_sensitive() && w.is_visible() {
match medium {
AddressType::Email => {
if let Some(ref action) = action {
match action {
AddressAction::Delete => println!("Delete email number"),
AddressAction::Add => println!("Add email number"),
}
}
},
AddressType::Phone => {
if let Some(ref action) = action {
match action {
AddressAction::Delete => println!("Delete phone number"),
AddressAction::Add => println!("Add phone number"),
}
}
},
};
}
});
}
}
......@@ -5,6 +5,7 @@ mod roomrow;
mod roomlist;
mod avatar;
mod autocomplete;
mod address;
pub mod divider;
pub mod image;
......@@ -12,6 +13,8 @@ pub use self::message::MessageBox;
pub use self::room::RoomBox;
pub use self::member::MemberBox;
pub use self::autocomplete::Autocomplete;
pub use self::address::Address;
pub use self::address::AddressType;
pub use self::roomrow::RoomRow;
pub use self::roomlist::RoomList;
pub use self::avatar::Avatar;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment