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 public static Progress prog = Progress.Create(
"Loading",
"",
"");
41 static bool useGPU =
true, useVips =
false, useImageSharp =
false;
42 public static bool UseGPU
52 useImageSharp = !value;
56 public static bool UseVips
65 useImageSharp = !value;
70 public static bool UseImageSharp
78 useImageSharp = value;
84 private static void UpdateStitching()
86 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
89 OpenSlideBase.useGPU =
false;
91 OpenSlideBase.UseVips =
false;
92 SlideBase.UseVips =
false;
95 OpenSlideBase.useGPU = useGPU;
97 OpenSlideBase.UseVips = useVips;
98 SlideBase.UseVips = useVips;
101 public static bool ToConsole
105 return BioLib.Recorder.outputConsole;
109 BioLib.Recorder.outputConsole = value;
112 static bool useFiji =
true;
113 public static bool UseFiji
117 if (OperatingSystem.IsMacOS())
123 if(Fiji.ImageJPath.Contains(
"Fiji"))
134 public static bool SetFijiOrImageJPath()
138 string st =
BioLib.Settings.GetSettings(
"ImageJPath");
141 BioLib.Fiji.ImageJPath = st;
142 BioLib.ImageJ.ImageJPath = st;
143 if (!OperatingSystem.IsMacOS())
146 ImageJ.Initialize(st);
150 string title =
"Select ImageJ Executable Location";
151 if (OperatingSystem.IsMacOS())
152 title =
"Select ImageJ Executable Location (Fiji.app/Contents/MacOS/ImageJ-macosx)";
153 Gtk.FileChooserDialog filechooser =
154 new Gtk.FileChooserDialog(title, Scripting.window,
155 FileChooserAction.Open,
156 "Cancel", ResponseType.Cancel,
157 "Save", ResponseType.Accept);
158 filechooser.SetCurrentFolder(System.IO.Path.GetDirectoryName(Environment.ProcessPath));
159 if (filechooser.Run() != (
int)ResponseType.Accept)
161 Fiji.ImageJPath = filechooser.Filename;
162 ImageJ.ImageJPath = filechooser.Filename;
163 if (!OperatingSystem.IsMacOS())
165 Fiji.Initialize(filechooser.Filename);
166 ImageJ.Initialize(filechooser.Filename);
168 BioLib.Settings.AddSettings(
"ImageJPath", filechooser.Filename);
169 filechooser.Destroy();
173 public static void MoveToMain(Window window)
175 MoveToMonitor(window, 0);
177 public static void MoveToMonitor(Window window,
int monitorIndex)
179 Display display = Display.Default;
183 int monitorCount = display.NMonitors;
185 if (monitorIndex >= monitorCount)
188 Monitor monitor = display.GetMonitor(monitorIndex);
189 Rectangle geometry = monitor.Geometry;
192 window.Move(geometry.X, geometry.Y);
196 public static void Initialize()
199 Console.WriteLine(
"Initializing components.");
200 SetFijiOrImageJPath();
201 if (!OperatingSystem.IsMacOS() && UseFiji)
203 Fiji.Initialize(Fiji.ImageJPath);
204 BioImage.Initialize(Fiji.ImageJPath);
206 else if (!OperatingSystem.IsMacOS())
208 ImageJ.Initialize(ImageJ.ImageJPath);
209 BioImage.Initialize(ImageJ.ImageJPath);
211 Console.WriteLine(
"Loading settings.");
213 updater = Updater.Create();
214 tools = Tools.Create();
215 filters = FiltersView.Create();
217 scripting = Scripting.Create();
218 funcs = Functions.Create();
219 progress = Progress.Create(
"",
"",
"");
220 tools= Tools.Create();
222 console = BioConsole.Create();
223 stack = StackTools.Create();
224 about = About.Create();
225 setTool = SetTool.Create();
226 recorder = Recorder.Create();
232 public static void ApplyStyles(Widget widget,
bool applyToContainer =
true,
bool applyColor =
true)
236 if (widget is not Button || widget is not SpinButton || widget is not Scrollbar && applyColor)
238 widget.ModifyBg(StateType.Normal,
new Gdk.Color(49, 91, 138));
239 widget.ModifyFg(StateType.Normal,
new Gdk.Color(255, 255, 255));
246 if (widget is Container && applyToContainer)
248 foreach (var child
in ((Container)widget).Children)
250 if(child is not Button || child is not SpinButton)
257 Console.WriteLine(e.Message.ToString());
259 if(widget is Gtk.Window)
261 var window = (Gtk.Window)widget;
262 window.Icon =
new Gdk.Pixbuf(
"bio.png");
263 window.DeleteEvent += Window_DeleteEvent;
264 window.DestroyEvent += Window_DestroyEvent;
265 window.FocusInEvent += Window_FocusInEvent;
268 private static void Window_FocusInEvent(
object o, FocusInEventArgs args)
270 Gtk.Window w = (Gtk.Window)o;
273 private static void Window_DestroyEvent(
object o, DestroyEventArgs args)
277 private static void Dialog_DeleteEvent(
object o, DeleteEventArgs args)
279 Dialog w = (Dialog)o;
284 private static void Dialog_DestroyEvent(
object o, DestroyEventArgs args)
287 private static void Window_DeleteEvent(
object o, DeleteEventArgs args)
289 Gtk.Window w = (Gtk.Window)o;
300 public static void SelectWindow(
string name)
302 TabsView tbs = App.tabsView;
304 int c = tbs.GetViewerCount();
305 for (
int v = 0; v < c; v++)
307 int cc = tbs.GetViewer(v).Images.Count;
308 for (
int im = 0; im < cc; im++)
310 if (tbs.GetViewer(v).Images[im].Filename == Path.GetFileName(name))
313 BioLib.Recorder.Record(
"App.SelectWindow(\"" + name +
"\")");
314 ImageView.SelectedImage = tbs.GetViewer(v).Images[im];
315 viewer = tbs.GetViewer(v);
323 public static void CloseWindow(
string name)
325 BioLib.Recorder.Record(
"App.CloseWindow(\"" + name +
"\")");
326 TabsView tbs = App.tabsView;
327 int c = tbs.GetViewerCount();
328 if (name ==
"ROI Manager")
329 App.roiManager.Close();
331 if (name ==
"Stack Tool")
334 if (name ==
"Channels Tool")
335 App.channelsTool.Close();
337 if (name ==
"Scripting")
338 App.scripting.Close();
340 if (name ==
"SAM Tool")
343 if (name ==
"Recorder")
344 App.recorder.Close();
346 if (name ==
"Color Tool")
349 for (
int v = 0; v < c; v++)
351 int cc = tbs.GetViewer(v).Images.Count;
352 for (
int im = 0; im < cc; im++)
354 ImageView vi = tbs.GetViewer(v);
355 if (vi.Images[im].Filename == Path.GetFileName(name) || vi.Title == name)
360 Images.RemoveImage(vi.Images[im]);
362 tbs.RemoveViewer(vi);
372 public static void Rename(
string text)
374 App.tabsView.RenameTab(ImageView.SelectedImage.Filename, text);
375 ImageView.SelectedImage.Rename(text);
376 ImageView v = App.tabsView.GetViewer(ImageView.SelectedImage.Filename);
377 v.SetTitle(ImageView.SelectedImage.Filename);
378 BioLib.Recorder.AddLine(
"App.Rename(\"" + text +
"\");",
false);
390 static MenuItem FindItem(Menu w,
string label)
392 foreach (Widget item
in w.Children)
398 for (
int i = 0; i < m.Children.Length; i++)
403 MenuItem it = (MenuItem)m.Children[i];
404 if (it.Label == label)
417 MenuItem it = (MenuItem)item;
418 if (it.Label == label)
442 private static void GetMainMenuItem(
string path, out Widget wid, out
bool Menu)
446 string[] s = path.Split(
'/');
447 MenuBar w = tabsView.MainMenu;
448 for (
int i = 0; i < s.Length; i++)
450 foreach (Widget item
in w.Children)
455 MenuItem m = FindItem((Menu)item, s[i]);
456 if(m !=
null && i == s.Length -1)
465 MenuItem mi = (MenuItem)item;
466 if (mi.Label == s[i])
476 GetMainImageJMenuItem(path, out wid, out Menu);
494 private static void GetMainImageJMenuItem(
string path, out Widget wid, out
bool Menu)
496 string[] s = path.Split(
'/');
497 MenuBar w = tabsView.ImageJMenu;
498 for (
int i = 0; i < s.Length; i++)
500 foreach (Widget item
in w.Children)
505 MenuItem m = FindItem((Menu)item, s[i]);
506 if (m !=
null && i == s.Length - 1)
515 MenuItem mi = (MenuItem)item;
516 if (mi.Label == s[i] && i == s.Length - 1)
544 private static void GetContextMenuItem(
string path, out Widget wid, out
bool Menu)
546 string[] s = path.Split(
'/');
547 Menu w = App.viewer.contextMenu;
548 for (
int i = 0; i < s.Length; i++)
550 foreach (Widget item
in w.Children)
555 MenuItem m = FindItem((Menu)item, s[i]);
556 if (m !=
null && i == s.Length - 1)
565 MenuItem mi = (MenuItem)item;
566 if (mi.Label == s[i] && i == s.Length - 1)
586 public static void AddMenu(
string path)
588 if (path ==
null || path ==
"")
593 GetMainMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
594 if(!menu && w !=
null)
596 MenuItem m = (MenuItem)w;
597 if(m.Submenu !=
null)
599 Menu me = (Menu)m.Submenu;
600 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
601 mi.ButtonPressEvent += ItemClicked;
609 Menu me =
new Menu();
610 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
611 mi.ButtonPressEvent += ItemClicked;
618 else if(menu && w!=
null)
622 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
623 mi.ButtonPressEvent += ItemClicked;
627 GetMainImageJMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
628 if (!menu && w !=
null)
630 MenuItem m = (MenuItem)w;
631 if (m.Submenu !=
null)
633 Menu me = (Menu)m.Submenu;
634 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
635 mi.ButtonPressEvent += ItemClicked;
642 Menu me =
new Menu();
643 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
644 mi.ButtonPressEvent += ItemClicked;
650 else if (menu && w !=
null)
654 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
655 mi.ButtonPressEvent += ItemClicked;
665 public static void AddContextMenu(
string path)
667 if (path ==
null || path ==
"")
669 string[] s = path.Split(
'/');
674 GetContextMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
675 if (!menu && w !=
null)
677 MenuItem m = (MenuItem)w;
678 if (m.Submenu !=
null)
680 Menu me = (Menu)m.Submenu;
681 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
682 mi.ButtonPressEvent += ItemClicked;
689 Menu me =
new Menu();
690 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
691 mi.ButtonPressEvent += ItemClicked;
697 else if (!menu && w ==
null)
699 MenuItem mi =
new MenuItem(System.IO.Path.GetFileName(path));
700 mi.ButtonPressEvent += ItemClicked;
701 viewer.contextMenu.Append(mi);
702 viewer.contextMenu.ShowAll();
714 private static void ItemClicked(
object o, ButtonPressEventArgs args)
716 MenuItem ts = (MenuItem)o;
717 if (Function.Functions.ContainsKey(ts.Label))
719 Function f = Function.Functions[ts.Label];
720 f.PerformFunction(
true);
724 if (ts.Label.EndsWith(
".dll"))
728 Plugin.Plugins[ts.Label].Execute(
new string[] { });
732 Console.WriteLine(e.Message.ToString());
735 else if(ts.Label.EndsWith(
".onnx") || ts.Label.EndsWith(
".pt"))
739 ML.ML.Run(ts.Label, ImageView.SelectedImage);
743 Console.WriteLine(e.Message.ToString());