Example #1
0
        static void Main(string[] args)
        {
#if !NET461
            Thread.CurrentThread.TrySetApartmentState(ApartmentState.STA);
#endif
            try
            {
                var   vm      = new MainWindowViewModel();
                var   factory = new DemoDockFactory();
                IDock layout  = null;

                string path = ModelSerializer.GetBasePath("Layout.json");
                if (ModelSerializer.Exists(path))
                {
                    layout = ModelSerializer.Load <RootDock>(path);
                }

                BuildAvaloniaApp().Start <MainWindow>(() =>
                {
                    // NOTE: Initialize layout after main window was created so child windows can be created.
                    vm.Factory = factory;
                    vm.Layout  = layout ?? vm.Factory.CreateLayout();
                    vm.Factory.InitLayout(vm.Layout, vm);
                    return(vm);
                });

                vm.Factory.CloseLayout(vm.Layout);

                ModelSerializer.Save(path, vm.Layout);
            }
            catch (Exception ex)
            {
                Print(ex);
            }
        }
Example #2
0
        public MainWindow()
        {
            this.InitializeComponent();
            this.AttachDevTools();

            this.FindControl <MenuItem>("FileNew").Click += (sender, e) =>
            {
                if (this.DataContext is MainWindowViewModel vm)
                {
                    if (vm.Layout is IDock root)
                    {
                        root.HideWindows();
                        if (root.CurrentView is IDock dock)
                        {
                            dock.HideWindows();
                        }
                    }
                    vm.Factory = new EmptyDockFactory();
                    vm.Layout  = vm.Factory.CreateLayout();
                    vm.Factory.InitLayout(vm.Layout, vm);
                }
            };

            this.FindControl <MenuItem>("FileOpen").Click += async(sender, e) =>
            {
                var dlg = new OpenFileDialog();
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "Json", Extensions = { "json" }
                });
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "All", Extensions = { "*" }
                });
                var result = await dlg.ShowAsync(this);

                if (result != null)
                {
                    if (this.DataContext is MainWindowViewModel vm)
                    {
                        IDock layout = ModelSerializer.Load <RootDock>(result.FirstOrDefault());
                        if (vm.Layout is IDock root)
                        {
                            root.HideWindows();
                            if (root.CurrentView is IDock dock)
                            {
                                dock.HideWindows();
                            }
                        }
                        vm.Layout = layout;
                        vm.Factory.InitLayout(vm.Layout, vm);
                    }
                }
            };

            this.FindControl <MenuItem>("FileSaveAs").Click += async(sender, e) =>
            {
                var dlg = new SaveFileDialog();
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "Json", Extensions = { "json" }
                });
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "All", Extensions = { "*" }
                });
                dlg.InitialFileName  = "Layout";
                dlg.DefaultExtension = "json";
                var result = await dlg.ShowAsync(this);

                if (result != null)
                {
                    if (this.DataContext is MainWindowViewModel vm)
                    {
                        ModelSerializer.Save(result, vm.Layout);
                    }
                }
            };

            this.FindControl <MenuItem>("FileGenerateCode").Click += async(sender, e) =>
            {
                var dlg = new SaveFileDialog();
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "C#", Extensions = { "cs" }
                });
                dlg.Filters.Add(new FileDialogFilter()
                {
                    Name = "All", Extensions = { "*" }
                });
                dlg.InitialFileName  = "ViewModel";
                dlg.DefaultExtension = "cs";
                var result = await dlg.ShowAsync(this);

                if (result != null)
                {
                    if (this.DataContext is MainWindowViewModel vm)
                    {
                        ICodeGen codeGeb = new CSharpCodeGen();
                        codeGeb.Generate(vm.Layout, result);
                    }
                }
            };

            this.FindControl <MenuItem>("ViewEditor").Click += (sender, e) =>
            {
                if (this.DataContext is MainWindowViewModel vm)
                {
                    var editorView = new EditorTool
                    {
                        Id     = "Editor",
                        Width  = double.NaN,
                        Height = double.NaN,
                        Title  = "Editor"
                    };

                    var layout = new ToolDock
                    {
                        Id          = nameof(IToolDock),
                        Dock        = "",
                        Width       = double.NaN,
                        Height      = double.NaN,
                        Title       = nameof(IToolDock),
                        CurrentView = editorView,
                        DefaultView = editorView,
                        Views       = vm.Factory.CreateList <IView>
                                      (
                            editorView
                                      )
                    };

                    vm.Factory.Update(layout, null, vm.Layout);

                    var window = vm.Factory.CreateWindowFrom(layout);
                    if (window != null)
                    {
                        if (vm.Layout is IDock root)
                        {
                            vm.Factory.AddWindow(root, window, null);
                        }
                        window.X      = 0;
                        window.Y      = 0;
                        window.Width  = 800;
                        window.Height = 600;
                        window.Present(false);
                    }
                }
            };
        }