11-08-2024, 03:37 PM
In any case this can be useful.
// script "Change toolbar button text2.cs"
using Au.Triggers;
using System.Windows.Threading;
class Program {
ConcurrentDictionary<string, MyToolbarData> _toolbars = new();
static void Main(string[] a) => new Program(a);
Program(string[] args) {
run.thread(_Thread);
var b = new wpfBuilder("Window").WinSize(400);
b.R.AddButton("Change toolbar button text", _ => { _ChangeButtonText(); });
b.R.AddOkCancel();
b.End();
if (!b.ShowDialog()) return;
}
void _Thread() {
var synCon = new DispatcherSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(synCon);
ActionTriggers Triggers = new();
Triggers.Options.ThreadOfTriggers();
Triggers.Window[TWEvent.VisibleOnce, "LibreAutomate", flags: TWFlags.RunAtStartup] = _Toolbar;
Triggers.Run();
void _Toolbar(WindowTriggerArgs ta) {
toolbar t = new("sgcqjw8d6dw", TBCtor.DontSaveSettings) { ActionThread = false };
t["Aaaaa"] = o => { };
t["Bbbbb"] = o => { };
TBItem button = t.Last;
t["Cccc"] = o => { };
_toolbars["unique name"] = new(t, button, synCon);
t.Closed += () => { _toolbars.Remove("unique name", out _); };
t.Show(ta);
}
}
void _ChangeButtonText() {
var q = _toolbars["unique name"];
q.threadContext.Post(_ => { //runs in the toolbar's thread
q.button.Text = "New longer text";
q.tb.DisplayText ^= true; q.tb.DisplayText ^= true; //a workaround to remeasure and redraw the toolbar. Don't need in next LA.
}, null);
}
}
record class MyToolbarData(toolbar tb, TBItem button, SynchronizationContext threadContext);