Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: GTK look and feel

Zack Bartel21fc5d5d6090
diff --git a/src/gtk/main.cpp b/src/gtk/main.cpp
index 294d6e4..0cfa594 100644
--- a/src/gtk/main.cpp
+++ b/src/gtk/main.cpp
@@ -28,6 +28,7 @@ namespace {
void activate(GtkApplication* app, gpointer) {
tether::Crypto::instance().init();
+ install_style();
GtkWidget* window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Tether");
diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cpp
index 70420f6..4307fad 100644
--- a/src/gtk/messages_view.cpp
+++ b/src/gtk/messages_view.cpp
@@ -3,6 +3,7 @@
#include "ui_util.hpp"
#include <ctime>
+#include <gdk/gdkkeysyms.h>
#include <set>
#include <string>
#include <vector>
@@ -17,6 +18,7 @@ namespace tether::ui {
GtkWidget* banner_action = nullptr;
GtkWidget* thread_list = nullptr;
GtkWidget* conversation = nullptr;
+ GtkWidget* conversation_header = nullptr;
GtkWidget* conversation_scroll = nullptr;
GtkWidget* composer = nullptr;
GtkWidget* send_button = nullptr;
@@ -37,6 +39,9 @@ namespace tether::ui {
// Handles already asked about, so reopening a conversation does not
// re-issue a blocking OBEX write per message every time.
std::set<std::string> marked_read;
+ // Scrolling to the newest message has to wait for GTK to lay the new
+ // rows out, so it is deferred rather than done inline.
+ guint scroll_idle_id = 0;
// Whether the daemon says the selected thread can be replied to, and
// why not when it cannot.
bool selected_repliable = false;
@@ -51,7 +56,10 @@ namespace tether::ui {
void update_composer_sensitivity();
void apply_row_selection(GtkWidget* row);
void clear_selection();
- void set_banner(const std::string& text);
+ // `offer_permissions` shows the button that re-solicits the iPhone's
+ // Bluetooth toggles, which only helps when that is the actual problem.
+ void set_banner(const std::string& text, bool offer_permissions = false);
+ std::string composer_text();
std::string format_timestamp(int64_t epoch) {
if (epoch <= 0)
@@ -129,6 +137,7 @@ namespace tether::ui {
GtkWidget* badge = gtk_label_new(nullptr);
gtk_label_set_markup(GTK_LABEL(badge), ("<b>" + std::to_string(unread) + "</b>").c_str());
gtk_widget_set_valign(badge, GTK_ALIGN_CENTER);
+ gtk_style_context_add_class(gtk_widget_get_style_context(badge), "tether-badge");
gtk_box_pack_start(GTK_BOX(box), badge, FALSE, FALSE, 0);
}
@@ -153,8 +162,14 @@ namespace tether::ui {
gtk_label_set_line_wrap(GTK_LABEL(bubble), TRUE);
gtk_label_set_line_wrap_mode(GTK_LABEL(bubble), PANGO_WRAP_WORD_CHAR);
gtk_label_set_max_width_chars(GTK_LABEL(bubble), 48);
- gtk_label_set_xalign(GTK_LABEL(bubble), outgoing ? 1.0 : 0.0);
+ gtk_label_set_xalign(GTK_LABEL(bubble), 0.0);
gtk_label_set_selectable(GTK_LABEL(bubble), TRUE);
+ // Without this the label stretches to whatever else is in the row and
+ // the bubble reads as a full-width bar rather than wrapping the text.
+ gtk_widget_set_halign(bubble, outgoing ? GTK_ALIGN_END : GTK_ALIGN_START);
+ GtkStyleContext* bubble_style = gtk_widget_get_style_context(bubble);
+ gtk_style_context_add_class(bubble_style, "tether-bubble");
+ gtk_style_context_add_class(bubble_style, outgoing ? "tether-bubble-out" : "tether-bubble-in");
gtk_box_pack_start(GTK_BOX(box), bubble, FALSE, FALSE, 0);
if (!stamp.empty()) {
@@ -168,13 +183,29 @@ namespace tether::ui {
return row;
}
- void scroll_conversation_to_end() {
+ gboolean scroll_to_end_idle(gpointer) {
+ g_messages.scroll_idle_id = 0;
if (!g_messages.conversation_scroll)
- return;
+ return G_SOURCE_REMOVE;
GtkAdjustment* adjustment =
gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(g_messages.conversation_scroll));
- if (adjustment)
- gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment));
+ if (adjustment) {
+ // upper is the full content height; subtracting the visible page
+ // is what puts the last message at the bottom edge rather than
+ // one screen past it.
+ gtk_adjustment_set_value(
+ adjustment, gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment));
+ }
+ return G_SOURCE_REMOVE;
+ }
+
+ void scroll_conversation_to_end() {
+ // The rows have only just been inserted, so GTK has not laid them out
+ // and the adjustment still reports the bounds of the old contents.
+ // Scrolling now lands somewhere short of the newest message.
+ if (g_messages.scroll_idle_id == 0)
+ g_messages.scroll_idle_id =
+ g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, scroll_to_end_idle, nullptr, nullptr);
}
void show_threads(const nlohmann::json& event) {
@@ -254,7 +285,7 @@ namespace tether::ui {
}
}
- void set_banner(const std::string& text) {
+ void set_banner(const std::string& text, bool offer_permissions) {
if (!g_messages.banner)
return;
if (text.empty()) {
@@ -263,6 +294,9 @@ namespace tether::ui {
}
set_text(g_messages.banner_label, text);
gtk_widget_show_all(g_messages.banner);
+ // show_all revealed it; hide it again unless re-soliciting is the
+ // thing that would actually help here.
+ gtk_widget_set_visible(g_messages.banner_action, offer_permissions);
}
// The advertisement that makes iOS reveal its Messages and Contacts
@@ -294,7 +328,11 @@ namespace tether::ui {
reason = event.value("link_reason", "");
if (reason.empty())
reason = "Messages are not connected.";
- set_banner(reason);
+
+ // Re-soliciting only helps when the phone is withholding the profile,
+ // not when the link itself is down.
+ const std::string map_error = event.value("map_error", "none");
+ set_banner(reason, map_error == "forbidden" || map_error == "no_record");
}
// Replying needs an open conversation and a live MAP session. A group
@@ -306,8 +344,11 @@ namespace tether::ui {
if (!g_messages.composer)
return;
- gtk_widget_set_sensitive(g_messages.composer, can_send && !g_messages.sending);
- gtk_widget_set_sensitive(g_messages.send_button, can_send && !g_messages.sending);
+ // The box stays typable whenever the conversation can take a reply;
+ // only the button waits for there to be something in it.
+ const bool composer_live = can_send && !g_messages.sending;
+ gtk_widget_set_sensitive(g_messages.composer, composer_live);
+ gtk_widget_set_sensitive(g_messages.send_button, composer_live && !composer_text().empty());
const char* reason = nullptr;
if (g_messages.sending)
@@ -375,6 +416,24 @@ namespace tether::ui {
set_status_main("Sending…");
}
+ // Every messaging app has trained people that Enter sends and Shift+Enter
+ // starts a new line. A multi-line box that only sends on a button click is
+ // the single thing that makes a composer feel unfinished.
+ gboolean on_composer_key(GtkWidget*, GdkEventKey* event, gpointer) {
+ if (event->keyval != GDK_KEY_Return && event->keyval != GDK_KEY_KP_Enter)
+ return FALSE;
+ if (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
+ return FALSE;
+ // Mid-composition input methods deliver Return as part of a candidate
+ // selection, which must not be read as a send.
+ if (event->state & GDK_MOD1_MASK)
+ return FALSE;
+
+ if (gtk_widget_is_sensitive(g_messages.send_button))
+ on_send_clicked(nullptr, nullptr);
+ return TRUE;
+ }
+
void on_send_result(const nlohmann::json& event) {
clear_sending();
update_composer_sensitivity();
@@ -409,6 +468,13 @@ namespace tether::ui {
g_messages.selected_name = name ? name : "";
g_messages.selected_repliable = g_object_get_data(G_OBJECT(row), "repliable") != nullptr;
g_messages.selected_block_reason = block_reason ? block_reason : "";
+ // Which conversation is open is otherwise only legible from the
+ // selection highlight in the list beside it.
+ set_markup(g_messages.conversation_header,
+ "<b>" +
+ escape_markup(g_messages.selected_name.empty() ? g_messages.selected_thread
+ : g_messages.selected_name) +
+ "</b>");
update_composer_sensitivity();
}
@@ -420,6 +486,10 @@ namespace tether::ui {
apply_row_selection(GTK_WIDGET(row));
gtk_stack_set_visible_child_name(GTK_STACK(g_messages.placeholder_stack), "conversation");
request_messages(g_messages.selected_thread);
+ // Only on a real click: the handler is blocked while the list is
+ // rebuilt, so this cannot steal focus behind the user's back.
+ if (gtk_widget_is_sensitive(g_messages.composer))
+ gtk_widget_grab_focus(g_messages.composer);
}
} // namespace
@@ -530,6 +600,15 @@ namespace tether::ui {
GtkWidget* conversation_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+ GtkWidget* conversation_header_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
+ gtk_container_set_border_width(GTK_CONTAINER(conversation_header_box), 10);
+ g_messages.conversation_header = gtk_label_new(nullptr);
+ gtk_label_set_xalign(GTK_LABEL(g_messages.conversation_header), 0.0);
+ gtk_label_set_ellipsize(GTK_LABEL(g_messages.conversation_header), PANGO_ELLIPSIZE_END);
+ gtk_box_pack_start(GTK_BOX(conversation_header_box), g_messages.conversation_header, TRUE, TRUE, 0);
+ gtk_box_pack_start(GTK_BOX(conversation_box), conversation_header_box, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(conversation_box), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
+
g_messages.conversation_scroll = gtk_scrolled_window_new(nullptr, nullptr);
gtk_scrolled_window_set_policy(
GTK_SCROLLED_WINDOW(g_messages.conversation_scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
@@ -542,17 +621,33 @@ namespace tether::ui {
gtk_container_set_border_width(GTK_CONTAINER(composer_box), 8);
g_messages.composer = gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(g_messages.composer), GTK_WRAP_WORD_CHAR);
+ gtk_text_view_set_left_margin(GTK_TEXT_VIEW(g_messages.composer), 6);
+ gtk_text_view_set_right_margin(GTK_TEXT_VIEW(g_messages.composer), 6);
+ gtk_text_view_set_top_margin(GTK_TEXT_VIEW(g_messages.composer), 6);
+ gtk_text_view_set_bottom_margin(GTK_TEXT_VIEW(g_messages.composer), 6);
gtk_widget_set_size_request(g_messages.composer, -1, 48);
GtkWidget* composer_frame = gtk_scrolled_window_new(nullptr, nullptr);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(composer_frame), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+ // Without a border the box floats in the panel with nothing marking it as
+ // somewhere to type.
+ gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(composer_frame), GTK_SHADOW_IN);
+ gtk_scrolled_window_set_max_content_height(GTK_SCROLLED_WINDOW(composer_frame), 140);
gtk_container_add(GTK_CONTAINER(composer_frame), g_messages.composer);
gtk_box_pack_start(GTK_BOX(composer_box), composer_frame, TRUE, TRUE, 0);
g_messages.send_button = gtk_button_new_with_label("Send");
gtk_widget_set_valign(g_messages.send_button, GTK_ALIGN_END);
+ gtk_style_context_add_class(gtk_widget_get_style_context(g_messages.send_button), "suggested-action");
gtk_box_pack_start(GTK_BOX(composer_box), g_messages.send_button, FALSE, FALSE, 0);
g_signal_connect(g_messages.send_button, "clicked", G_CALLBACK(on_send_clicked), nullptr);
+ g_signal_connect(g_messages.composer, "key-press-event", G_CALLBACK(on_composer_key), nullptr);
+ // The button follows what is actually in the box, so "Send" is never
+ // offered for an empty message.
+ g_signal_connect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_messages.composer)),
+ "changed",
+ G_CALLBACK(+[](GtkTextBuffer*, gpointer) { update_composer_sensitivity(); }),
+ nullptr);
// Enabled only once MAP is up and a conversation is open.
update_composer_sensitivity();
diff --git a/src/gtk/ui_util.cpp b/src/gtk/ui_util.cpp
index 7dffe2a..54f080e 100644
--- a/src/gtk/ui_util.cpp
+++ b/src/gtk/ui_util.cpp
@@ -5,8 +5,52 @@ namespace tether::ui {
namespace {
GtkWidget* g_window = nullptr;
GtkWidget* g_header_bar = nullptr;
+
+ constexpr const char* STYLE = R"CSS(
+.muted {
+ opacity: 0.62;
+ font-size: 90%;
+}
+
+.tether-bubble {
+ padding: 8px 12px;
+ border-radius: 14px;
+}
+
+.tether-bubble-in {
+ background-color: alpha(@theme_fg_color, 0.10);
+}
+
+.tether-bubble-out {
+ background-color: @theme_selected_bg_color;
+ color: @theme_selected_fg_color;
+}
+
+.tether-badge {
+ background-color: @theme_selected_bg_color;
+ color: @theme_selected_fg_color;
+ border-radius: 10px;
+ padding: 0 8px;
+}
+)CSS";
} // namespace
+ void install_style() {
+ GdkScreen* screen = gdk_screen_get_default();
+ if (!screen)
+ return;
+
+ GtkCssProvider* provider = gtk_css_provider_new();
+ GError* error = nullptr;
+ if (!gtk_css_provider_load_from_data(provider, STYLE, -1, &error)) {
+ g_warning("tether: stylesheet rejected: %s", error ? error->message : "unknown");
+ g_clear_error(&error);
+ }
+ gtk_style_context_add_provider_for_screen(
+ screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+ g_object_unref(provider);
+ }
+
std::string escape_markup(const std::string& text) {
gchar* escaped = g_markup_escape_text(text.c_str(), -1);
std::string result = escaped ? escaped : "";
diff --git a/src/gtk/ui_util.hpp b/src/gtk/ui_util.hpp
index a298715..e4e4200 100644
--- a/src/gtk/ui_util.hpp
+++ b/src/gtk/ui_util.hpp
@@ -5,6 +5,11 @@
namespace tether::ui {
+ // Loads the application stylesheet. Every view already tags labels with
+ // "muted"; until this is installed nothing defines that class, so the tag
+ // has no effect at all.
+ void install_style();
+
std::string escape_markup(const std::string& text);
void set_markup(GtkWidget* label, const std::string& text);
void set_text(GtkWidget* label, const std::string& text);
diff --git a/test/test_bmessage.cpp b/test/test_bmessage.cpp
index 34a811b..c5b6024 100644
--- a/test/test_bmessage.cpp
+++ b/test/test_bmessage.cpp
@@ -384,3 +384,130 @@ TEST(MapListingConversion, KeepsTheObjectPathForActingOnTheMessage) {
EXPECT_EQ(message.handle, "42");
EXPECT_EQ(message.object_path, "/org/bluez/obex/client/session5/message42");
}
+
+namespace {
+
+ MapListing listing_for(const std::string& path, bool sent, const std::string& folder) {
+ MapListing listing;
+ listing.handle = path;
+ listing.subject = "see you at six";
+ listing.sender_name = "Alice";
+ listing.sender_address = "+15551110000";
+ listing.recipient_name = "Bob";
+ listing.recipient_address = "+15552220000";
+ listing.timestamp = "20260817T190000";
+ listing.sent = sent;
+ listing.folder = folder;
+ return listing;
+ }
+
+} // namespace
+
+// A received message is identified by whoever sent it.
+TEST(MapListing, IncomingIsKeyedOnTheSender) {
+ const Message message = message_from_listing(listing_for("/obex/session1/message1", false, "inbox"));
+ EXPECT_FALSE(message.outgoing);
+ EXPECT_EQ(message.thread_key, "tel:+15551110000");
+ EXPECT_EQ(message.peer_name, "Alice");
+}
+
+// A message the phone sent names the user as its sender. Keying on that would
+// collapse every outgoing message into one thread under the user's own number,
+// and would aim a reply there too.
+TEST(MapListing, SentIsKeyedOnTheRecipient) {
+ const Message message = message_from_listing(listing_for("/obex/session1/message2", true, "sent"));
+ EXPECT_TRUE(message.outgoing);
+ EXPECT_EQ(message.thread_key, "tel:+15552220000");
+ EXPECT_EQ(message.peer_name, "Bob");
+}
+
+// Not every phone sets the Sent flag on what it serves from the sent folder.
+TEST(MapListing, SentFolderIsOutgoingWithoutTheFlag) {
+ const Message message = message_from_listing(listing_for("/obex/session1/message3", false, "telecom/msg/sent"));
+ EXPECT_TRUE(message.outgoing);
+ EXPECT_EQ(message.thread_key, "tel:+15552220000");
+}
+
+// An address we do not have is not an address to guess at: an empty thread key
+// is the signal for the caller to drop the message rather than misfile it.
+TEST(MapListing, SentWithoutARecipientHasNoThread) {
+ MapListing listing = listing_for("/obex/session1/message4", true, "sent");
+ listing.recipient_address.clear();
+ listing.recipient_name.clear();
+
+ const Message message = message_from_listing(listing);
+ EXPECT_TRUE(message.outgoing);
+ EXPECT_TRUE(message.thread_key.empty());
+}
+
+TEST(MapListing, EmailRecipientIsKeyedAsAnAppleId) {
+ MapListing listing = listing_for("/obex/session1/message5", true, "sent");
+ listing.recipient_address = "[email protected]";
+
+ const Message message = message_from_listing(listing);
+ EXPECT_EQ(message.thread_key, "email:[email protected]");
+}
+
+namespace {
+
+ Message local_send(const std::string& thread, const std::string& body, int64_t ts) {
+ Message m;
+ m.handle = std::string(LOCAL_HANDLE_PREFIX) + std::to_string(ts);
+ m.thread_key = thread;
+ m.peer_address = thread;
+ m.body = body;
+ m.timestamp = ts;
+ m.outgoing = true;
+ m.read = true;
+ m.folder = "outbox";
+ return m;
+ }
+
+ Message phone_copy(const std::string& handle, const std::string& thread, const std::string& body, int64_t ts) {
+ Message m = local_send(thread, body, ts);
+ m.handle = handle;
+ m.folder = "sent";
+ return m;
+ }
+
+} // namespace
+
+// A reply sent from Tether is shown immediately under a local handle. Once the
+// phone lists its own copy from the sent folder, the two must collapse into one
+// message rather than showing the reply twice.
+TEST(MessageStore, PhoneCopySupersedesTheLocalEcho) {
+ MessageStore store;
+ ASSERT_TRUE(store.add(local_send("tel:+1111", "on my way", 1000)));
+ ASSERT_TRUE(store.add(phone_copy("m9", "tel:+1111", "on my way", 1005)));
+
+ EXPECT_EQ(store.size(), 1u);
+ auto messages = store.messages("tel:+1111");
+ ASSERT_EQ(messages.size(), 1u);
+ EXPECT_EQ(messages[0].handle, "m9") << "the phone's copy is the one with a usable handle";
+}
+
+TEST(MessageStore, UnrelatedOutgoingMessagesAreNotCollapsed) {
+ MessageStore store;
+ store.add(local_send("tel:+1111", "on my way", 1000));
+
+ // Same text, different conversation.
+ store.add(phone_copy("m1", "tel:+2222", "on my way", 1005));
+ // Same conversation, different text.
+ store.add(phone_copy("m2", "tel:+1111", "running late", 1005));
+ // Same text and conversation, but far too long afterwards to be the echo.
+ store.add(phone_copy("m3", "tel:+1111", "on my way", 1000 + LOCAL_ECHO_WINDOW_SECONDS + 1));
+
+ EXPECT_EQ(store.size(), 4u) << "a distinct message was mistaken for a duplicate";
+}
+
+// The journal replays the placeholder and the phone's copy in the order they
+// were written, so a restart must collapse them the same way.
+TEST(MessageStore, EchoIsCollapsedOnReplayToo) {
+ MessageStore store;
+ store.add(local_send("tel:+1111", "on my way", 1000));
+ store.add(phone_copy("m9", "tel:+1111", "on my way", 1005));
+ store.add(phone_copy("m9", "tel:+1111", "on my way", 1005));
+
+ EXPECT_EQ(store.size(), 1u);
+ EXPECT_EQ(store.threads().size(), 1u);
+}