5using org.checkerframework.common.returnsreceiver.qual;
7using System.Collections.Generic;
9using System.Runtime.InteropServices;
11using System.Threading.Tasks;
12using Monitor = Gdk.Monitor;
13using Window = Gtk.Window;
17 public static class App
19 public static Tools tools;
20 public static TabsView tabsView;
21 public static ImageView viewer;
22 public static ColorTool color;
23 public static Progress progress;
24 public static About about;
25 public static NodeView nodeView;
26 public static FiltersView filters;
27 public static ApplyFilter applyFilter;
28 public static ChannelsTool channelsTool;
30 public static Scripting scripting;
31 public static BioConsole console;
32 public static Functions funcs;
33 public static StackTools stack;
34 public static SetTool setTool;
35 public static Resolutions resolutions;
36 public static Recorder recorder;
37 public static SAMTool samTool;
38 public static Updater updater;
39 private static int selectedIndex;
40 static bool useGPU =
true, useVips =
false, useImageSharp =
false;
41 public static bool UseGPU
51 useImageSharp = !value;
55 public static bool UseVips
64 useImageSharp = !value;
69 public static bool UseImageSharp
77 useImageSharp = value;
83 private static void UpdateStitching()
85 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
88 OpenSlideBase.useGPU =
false;
90 OpenSlideBase.UseVips =
false;
91 SlideBase.UseVips =
false;
94 OpenSlideBase.useGPU = useGPU;
96 OpenSlideBase.UseVips = useVips;
97 SlideBase.UseVips = useVips;
100 public static bool ToConsole
104 return BioLib.Recorder.outputConsole;
108 BioLib.Recorder.outputConsole = value;
111 static bool useFiji =
true;
112 public static bool UseFiji
116 if (OperatingSystem.IsMacOS())
122 if(Fiji.ImageJPath.Contains(
"Fiji"))
133 public static bool SetFijiOrImageJPath()
136 BioLib.Settings.Load();
137 string st = BioLib.Settings.GetSettings(
"ImageJPath");
140 if (st.Contains(
"Fiji"))
142 BioLib.Fiji.ImageJPath = st;
145 BioLib.ImageJ.ImageJPath = st;
148 string title =
"Select ImageJ Executable Location";
149 if (OperatingSystem.IsMacOS())
150 title =
"Select ImageJ Executable Location (Fiji.app/Contents/MacOS/ImageJ-macosx)";
151 Gtk.FileChooserDialog filechooser =
152 new Gtk.FileChooserDialog(title, Scripting.window,
153 FileChooserAction.Open,
154 "Cancel", ResponseType.Cancel,
155 "Save", ResponseType.Accept);
156 filechooser.SetCurrentFolder(System.IO.Path.GetDirectoryName(Environment.ProcessPath));
157 if (filechooser.Run() != (
int)ResponseType.Accept)
159 if(filechooser.Filename.Contains(
"Fiji"))
160 Fiji.ImageJPath = filechooser.Filename;
162 ImageJ.ImageJPath = filechooser.Filename;
163 BioLib.Settings.AddSettings(
"ImageJPath", filechooser.Filename);
164 filechooser.Destroy();
165 BioLib.Settings.Save();
168 public static void MoveToMain(Window window)
170 MoveToMonitor(window, 0);
172 public static void MoveToMonitor(Window window,
int monitorIndex)
174 Display display = Display.Default;
178 int monitorCount = display.NMonitors;
180 if (monitorIndex >= monitorCount)
183 Monitor monitor = display.GetMonitor(monitorIndex);
184 Rectangle geometry = monitor.Geometry;
187 window.Move(geometry.X, geometry.Y);
191 public static void Initialize()
193 Console.WriteLine(
"Initializing components.");
194 SetFijiOrImageJPath();
195 if (Fiji.ImageJPath ==
"")
197 ImageJ.Initialize(ImageJ.ImageJPath);
198 BioImage.Initialize(ImageJ.ImageJPath);
202 Fiji.Initialize(Fiji.ImageJPath);
203 BioImage.Initialize(Fiji.ImageJPath);
205 Console.WriteLine(
"Loading settings.");
206 BioLib.Settings.Load();
207 updater = Updater.Create();
208 tools = Tools.Create();
209 filters = FiltersView.Create();
211 scripting = Scripting.Create();
212 funcs = Functions.Create();
213 progress = Progress.Create(
"",
"",
"");
214 tools= Tools.Create();
216 console = BioConsole.Create();
217 stack = StackTools.Create();
218 about = About.Create();
219 setTool = SetTool.Create();
220 recorder = Recorder.Create();
223 BioLib.Settings.Load();
226 public static void ApplyStyles(Widget widget)
231 if (widget is not Button || widget is not SpinButton)
233 widget.ModifyBg(StateType.Normal,
new Gdk.Color(49, 91, 138));
234 widget.ModifyFg(StateType.Normal,
new Gdk.Color(255, 255, 255));
239 if (widget is Container)
241 foreach (var child
in ((Container)widget).Children)
243 if(child is not Button || child is not SpinButton)
250 Console.WriteLine(e.Message.ToString());
252 if(widget is Gtk.Window)
254 var window = (Gtk.Window)widget;
255 window.Icon =
new Gdk.Pixbuf(
"bio.png");
256 window.DeleteEvent += Window_DeleteEvent;
257 window.DestroyEvent += Window_DestroyEvent;
258 window.FocusInEvent += Window_FocusInEvent;
261 private static void Window_FocusInEvent(
object o, FocusInEventArgs args)
263 Gtk.Window w = (Gtk.Window)o;
266 private static void Window_DestroyEvent(
object o, DestroyEventArgs args)
268 Gtk.Window w = (Gtk.Window)o;
273 private static void Dialog_DeleteEvent(
object o, DeleteEventArgs args)
275 Dialog w = (Dialog)o;
279 private static void Dialog_DestroyEvent(
object o, DestroyEventArgs args)
281 Dialog w = (Dialog)o;
285 private static void Window_DeleteEvent(
object o, DeleteEventArgs args)
287 Gtk.Window w = (Gtk.Window)o;
292 public static void SelectWindow(
string name)
294 TabsView tbs = App.tabsView;
296 int c = tbs.GetViewerCount();
297 for (
int v = 0; v < c; v++)
299 int cc = tbs.GetViewer(v).Images.Count;
300 for (
int im = 0; im < cc; im++)
302 if (tbs.GetViewer(v).Images[im].Filename == Path.GetFileName(name))
305 BioLib.Recorder.Record(
"App.SelectWindow(\"" + name +
"\")");
306 ImageView.SelectedImage = tbs.GetViewer(v).Images[im];
307 viewer = tbs.GetViewer(v);
315 public static void CloseWindow(
string name)
317 BioLib.Recorder.Record(
"App.CloseWindow(\"" + name +
"\")");
318 TabsView tbs = App.tabsView;
319 int c = tbs.GetViewerCount();
320 if (name ==
"ROI Manager")
321 App.roiManager.Close();
323 if (name ==
"Stack Tool")
326 if (name ==
"Channels Tool")
327 App.channelsTool.Close();
329 if (name ==
"Scripting")
330 App.scripting.Close();
332 if (name ==
"SAM Tool")
335 if (name ==
"Recorder")
336 App.recorder.Close();
338 if (name ==
"Color Tool")
341 for (
int v = 0; v < c; v++)
343 int cc = tbs.GetViewer(v).Images.Count;
344 for (
int im = 0; im < cc; im++)
346 ImageView vi = tbs.GetViewer(v);
347 if (vi.Images[im].Filename == Path.GetFileName(name) || vi.Title == name)
352 Images.RemoveImage(vi.Images[im]);
360 public static void Rename(
string text)
362 App.tabsView.RenameTab(ImageView.SelectedImage.Filename, text);
363 ImageView.SelectedImage.Rename(text);
364 ImageView v = App.tabsView.GetViewer(ImageView.SelectedImage.Filename);
365 v.SetTitle(ImageView.SelectedImage.Filename);
366 BioLib.Recorder.AddLine(
"App.Rename(\"" + text +
"\");",
false);
378 static MenuItem FindItem(Menu w,
string label)
380 foreach (Widget item
in w.Children)
386 for (
int i = 0; i < m.Children.Length; i++)
391 MenuItem it = (MenuItem)m.Children[i];
392 if (it.Label == label)
405 MenuItem it = (MenuItem)item;
406 if (it.Label == label)
430 private static void GetMainMenuItem(
string path, out Widget wid, out
bool Menu)
434 string[] s = path.Split(
'/');
435 MenuBar w = tabsView.MainMenu;
436 for (
int i = 0; i < s.Length; i++)
438 foreach (Widget item
in w.Children)
443 MenuItem m = FindItem((Menu)item, s[i]);
444 if(m !=
null && i == s.Length -1)
453 MenuItem mi = (MenuItem)item;
454 if (mi.Label == s[i])
464 GetMainImageJMenuItem(path, out wid, out Menu);
482 private static void GetMainImageJMenuItem(
string path, out Widget wid, out
bool Menu)
484 string[] s = path.Split(
'/');
485 MenuBar w = tabsView.ImageJMenu;
486 for (
int i = 0; i < s.Length; i++)
488 foreach (Widget item
in w.Children)
493 MenuItem m = FindItem((Menu)item, s[i]);
494 if (m !=
null && i == s.Length - 1)
503 MenuItem mi = (MenuItem)item;
504 if (mi.Label == s[i] && i == s.Length - 1)
532 private static void GetContextMenuItem(
string path, out Widget wid, out
bool Menu)
534 string[] s = path.Split(
'/');
535 Menu w = App.viewer.contextMenu;
536 for (
int i = 0; i < s.Length; i++)
538 foreach (Widget item
in w.Children)
543 MenuItem m = FindItem((Menu)item, s[i]);
544 if (m !=
null && i == s.Length - 1)
553 MenuItem mi = (MenuItem)item;
554 if (mi.Label == s[i] && i == s.Length - 1)
574 public static void AddMenu(
string path)
576 if (path ==
null || path ==
"")
581 GetMainMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
582 if(!menu && w !=
null)
584 MenuItem m = (MenuItem)w;
585 if(m.Submenu !=
null)
587 Menu me = (Menu)m.Submenu;
588 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
589 mi.ButtonPressEvent += ItemClicked;
597 Menu me =
new Menu();
598 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
599 mi.ButtonPressEvent += ItemClicked;
606 else if(menu && w!=
null)
610 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
611 mi.ButtonPressEvent += ItemClicked;
615 GetMainImageJMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
616 if (!menu && w !=
null)
618 MenuItem m = (MenuItem)w;
619 if (m.Submenu !=
null)
621 Menu me = (Menu)m.Submenu;
622 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
623 mi.ButtonPressEvent += ItemClicked;
630 Menu me =
new Menu();
631 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
632 mi.ButtonPressEvent += ItemClicked;
638 else if (menu && w !=
null)
642 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
643 mi.ButtonPressEvent += ItemClicked;
653 public static void AddContextMenu(
string path)
655 if (path ==
null || path ==
"")
657 string[] s = path.Split(
'/');
662 GetContextMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
663 if (!menu && w !=
null)
665 MenuItem m = (MenuItem)w;
666 if (m.Submenu !=
null)
668 Menu me = (Menu)m.Submenu;
669 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
670 mi.ButtonPressEvent += ItemClicked;
677 Menu me =
new Menu();
678 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
679 mi.ButtonPressEvent += ItemClicked;
685 else if (!menu && w ==
null)
687 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
688 mi.ButtonPressEvent += ItemClicked;
689 viewer.contextMenu.Append(mi);
690 viewer.contextMenu.ShowAll();
702 private static void ItemClicked(
object o, ButtonPressEventArgs args)
704 MenuItem ts = (MenuItem)o;
705 if (Function.Functions.ContainsKey(ts.Label))
707 Function f = Function.Functions[ts.Label];
708 f.PerformFunction(
true);
712 if (ts.Label.EndsWith(
".dll"))
716 Plugin.Plugins[ts.Label].Execute(
new string[] { });
720 Console.WriteLine(e.Message.ToString());
723 else if(ts.Label.EndsWith(
".onnx") || ts.Label.EndsWith(
".pt"))
727 ML.ML.Run(ts.Label, ImageView.SelectedImage);
731 Console.WriteLine(e.Message.ToString());