diff --git a/program/Appconfig.cs b/program/Appconfig.cs index e56a470..8eb2fec 100644 --- a/program/Appconfig.cs +++ b/program/Appconfig.cs @@ -3,7 +3,18 @@ public static class AppConfig { - // Computed once, cached forever — reading it anywhere costs nothing. - public static readonly bool DebugMode = - Environment.GetCommandLineArgs().Contains("--debug"); + // // Computed once, cached forever — reading it anywhere costs nothing. + // public static readonly bool DebugMode = + // Environment.GetCommandLineArgs().Contains("--debug"); + + public static readonly bool DebugMode = Compute(); + + private static bool Compute() + { + var argv = Environment.GetCommandLineArgs(); + Console.WriteLine("argv = [" + string.Join(" | ", argv) + "]"); + return argv.Contains("--debug") + || Environment.GetEnvironmentVariable("EARS_DEBUG") == "1"; + } + } \ No newline at end of file diff --git a/program/MainWindow.cs b/program/MainWindow.cs index 57012d9..b7828c5 100644 --- a/program/MainWindow.cs +++ b/program/MainWindow.cs @@ -28,6 +28,12 @@ [UI] private Button SettingButton = null; [UI] private Image ConnectionIcon = null; // was GtkIconView in the old glade + [UI] private Label ConnectionAlertLabel = null; + + private uint _retryTimerId; + private bool _probing; + private const uint RetryMs = 1000; + private CssProvider _scaleCssProvider; // private CssProvider _languageCssProvider; private int _lastAppliedWidth = -1; @@ -122,6 +128,8 @@ _Language.Register("LanguageLabel", LanguageLabel); _Language.Register("LanguageToggle", LanguageToggle); _Language.RegisterDynamic("ConnectionIcon", ConnectionIcon); + ConnectionAlertLabel.Name = "ConnectionAlertLabel"; + _Language.Register("ConnectionAlertLabel", ConnectionAlertLabel); _settingsWindow = new SettingsWindow(builder, _arduino, _Language); _settingsWindow.BackRequested += OnSettingsClosed; @@ -204,7 +212,18 @@ Option1Button.Sensitive = allowFullAccess; Option2Button.Sensitive = allowFullAccess; Option3Button.Sensitive = allowFullAccess; - SettingButton.Sensitive = true; // always reachable, device or not + // SettingButton.Sensitive = true; // always reachable, device or not + + if (ConnectionAlertLabel != null) + { + ConnectionAlertLabel.Visible = !connected; + var ctx = ConnectionAlertLabel.StyleContext; + if (connected) ctx.RemoveClass("alert"); + else ctx.AddClass("alert"); + } + + if (connected || AppConfig.DebugMode) StopAutoRetry(); + else StartAutoRetry(); if (ConnectionIcon == null) return; @@ -217,6 +236,41 @@ : _Language["ConnectionIcon.Disconnected"]; } + private void StartAutoRetry() + { + if (_retryTimerId != 0) return; + _retryTimerId = GLib.Timeout.Add(RetryMs, OnRetryTick); + } + + private void StopAutoRetry() + { + if (_retryTimerId != 0) + { + GLib.Source.Remove(_retryTimerId); + _retryTimerId = 0; + } + } + + private bool OnRetryTick() + { + if (_arduino.IsOpen) { _retryTimerId = 0; return false; } + if (_probing) return true; // scan still running — don't stack them + + _probing = true; + _arduino.DetectPortAsync().ContinueWith(t => + { + string port = t.IsFaulted ? null : t.Result; + // DetectPortAsync runs off the UI thread; Connect() touches GLib. + Gtk.Application.Invoke((s, e) => + { + _probing = false; + if (port != null && !_arduino.IsOpen) + _arduino.Connect(port); // fires ConnectionChanged(true) + }); + }); + return true; + } + // ---------- localisation ---------- // private Dictionary LoadButtonText(string path) diff --git a/program/homepageallv7.glade b/program/homepageallv7.glade index 0b06dc9..0d81a6d 100644 --- a/program/homepageallv7.glade +++ b/program/homepageallv7.glade @@ -164,6 +164,21 @@ 3 + + + True + False + center + center + True + Problem connecting with device + + + 0 + 4 + 2 + + diff --git a/program/info_eng.csv b/program/info_eng.csv index 04cbb7e..02440d3 100644 --- a/program/info_eng.csv +++ b/program/info_eng.csv @@ -25,4 +25,5 @@ Log.EspNowConnected,[ESP-NOW] Peer connected Log.EspNowDisconnected,[ESP-NOW] Peer disconnected SoundBackButton,Back -MapBackButton,Back \ No newline at end of file +MapBackButton,Back +ConnectionAlertLabel,Problem connecting with device \ No newline at end of file diff --git a/program/info_jpn.csv b/program/info_jpn.csv index a7ec241..23de4cb 100644 --- a/program/info_jpn.csv +++ b/program/info_jpn.csv @@ -25,4 +25,5 @@ Log.EspNowConnected,[ESP-NOW] 相手が接続しました Log.EspNowDisconnected,[ESP-NOW] 相手が切断しました SoundBackButton,戻る -MapBackButton,戻る \ No newline at end of file +MapBackButton,戻る +ConnectionAlertLabel,デバイスに接続できません \ No newline at end of file diff --git a/program/touch_friendly.css b/program/touch_friendly.css index 3284f41..207a7d8 100644 --- a/program/touch_friendly.css +++ b/program/touch_friendly.css @@ -188,4 +188,24 @@ background-color: #ffffff; font-size: 16px; } -window.popup list row label { color: #111111; } \ No newline at end of file +window.popup list row label { color: #111111; } + +/* inactive buttons */ +/* splash: modes unavailable until the device connects */ +#Option1Button:disabled, #Option2Button:disabled, #Option3Button:disabled { + background-color: #A8AFA0; + border-color: rgba(0,0,0,0.25); +} +#Option1Button:disabled label, +#Option2Button:disabled label, +#Option3Button:disabled label { color: #EFEFEF; } + +#ConnectionAlertLabel { + font-family: 'Noto Sans JP', 'IPAPGothic', sans-serif; + font-size: 22px; + font-weight: 700; + color: #ffffff; + background-color: #C62828; + padding: 10px 24px; + margin: 8px 40px; +} \ No newline at end of file