Пример #1
0
        private void InitializeTheme()
        {
            switch (GeneralSettings.Settings.Theme)
            {
            case "Dark":
            case "Mysterious":
            case "Turquoise":
            case "Emerald":
            case "Light":
                Current.Styles[1] = new StyleInclude(new Uri("resm:Styles?assembly=Regul"))
                {
                    Source = new Uri($"avares://PleasantUI/Assets/Themes/{GeneralSettings.Settings.Theme}.axaml")
                };
                break;

            default:
                List <Theme> Themes = new List <Theme>();

                if (Directory.Exists(CorePaths.Themes))
                {
                    foreach (string path in Directory.EnumerateFiles(CorePaths.Themes, "*.xml"))
                    {
                        using (FileStream fs = File.OpenRead(path))
                            Themes.Add((Theme) new XmlSerializer(typeof(Theme)).Deserialize(fs));
                    }
                }

                Theme theme = Themes.FirstOrDefault(t => t.Name == GeneralSettings.Settings.Theme);
                if (theme != null)
                {
                    Current.Styles[1] = AvaloniaRuntimeXamlLoader.Parse <IStyle>(theme.ToAxaml());
                }
                else
                {
                    Current.Styles[1] = new StyleInclude(new Uri("resm:Styles?assembly=Regul"))
                    {
                        Source = new Uri("avares://PleasantUI/Assets/Themes/Light.axaml")
                    }
                };

                break;
            }
        }
Пример #2
0
        public void ContentControl_ContentTemplate_Is_Functional()
        {
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui'>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <TextBlock Text='Foo' />
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>";

            var contentControl = AvaloniaRuntimeXamlLoader.Parse <ContentControl>(xaml);
            var target         = contentControl.ContentTemplate;

            Assert.NotNull(target);

            var txt = (TextBlock)target.Build(null);

            Assert.Equal("Foo", txt.Text);
        }
Пример #3
0
        public void Multi_Xaml_Binding_Is_Parsed()
        {
            var xaml =
                @"<MultiBinding xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    Converter ='{x:Static BoolConverters.And}'>
     <Binding Path='Foo' />
     <Binding Path='Bar' />
</MultiBinding>";

            var target = AvaloniaRuntimeXamlLoader.Parse <MultiBinding>(xaml);

            Assert.Equal(2, target.Bindings.Count);

            Assert.Equal(BoolConverters.And, target.Converter);

            var bindings = target.Bindings.Cast <Binding>().ToArray();

            Assert.Equal("Foo", bindings[0].Path);
            Assert.Equal("Bar", bindings[1].Path);
        }
Пример #4
0
        public void Should_Parse_Tip_With_Comment()
        {
            var xaml = @"
                <TextBlock xmlns='https://github.com/avaloniaui' Text='TextBlock with tooltip'>
                    <ToolTip.Tip>
                        <!--Comment-->
                        <ToolTip>
                            Foo
                        </ToolTip>
                    </ToolTip.Tip>
                </TextBlock>";

            var textBlock = AvaloniaRuntimeXamlLoader.Parse <TextBlock>(xaml);

            var toolTip = ToolTip.GetTip(textBlock) as ToolTip;

            Assert.NotNull(toolTip);

            Assert.Equal("Foo", toolTip.Content);
        }
Пример #5
0
        public void ControlTemplate_With_Nested_Child_Is_Operational()
        {
            var xaml     = @"
<ControlTemplate xmlns='https://github.com/avaloniaui'>
    <ContentControl Name='parent'>
        <ContentControl Name='child' />
    </ContentControl>
</ControlTemplate>
";
            var template = AvaloniaRuntimeXamlLoader.Parse <ControlTemplate>(xaml);

            var parent = (ContentControl)template.Build(new ContentControl()).Control;

            Assert.Equal("parent", parent.Name);

            var child = parent.Content as ContentControl;

            Assert.NotNull(child);

            Assert.Equal("child", child.Name);
        }
Пример #6
0
        public void All_Properties_Are_Set_Before_Final_EndInit()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <local:InitializationOrderTracker Width='100' Height='100'
        Tag='{Binding Height, RelativeSource={RelativeSource Self}}' />
</Window>";


                var window  = AvaloniaRuntimeXamlLoader.Parse <Window>(xaml);
                var tracker = (InitializationOrderTracker)window.Content;

                //ensure binding is set and operational first
                Assert.Equal(100.0, tracker.Tag);

                Assert.Equal("EndInit 0", tracker.Order.Last());
            }
        }
Пример #7
0
        public void DeferedXamlLoader_Should_Preserve_NamespacesContext()
        {
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <TextBlock  Tag='{x:Static local:NonControl.StringProperty}'/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>";

            var contentControl = AvaloniaRuntimeXamlLoader.Parse <ContentControl>(xaml);
            var template       = contentControl.ContentTemplate;

            Assert.NotNull(template);

            var txt = (TextBlock)template.Build(null);

            Assert.Equal((object)NonControl.StringProperty, txt.Tag);
        }
Пример #8
0
        public void Panel_Children_Are_Added()
        {
            var xaml = @"
<UserControl xmlns='https://github.com/avaloniaui'>
    <Panel Name='panel'>
        <ContentControl Name='Foo' />
        <ContentControl Name='Bar' />
    </Panel>
</UserControl>";

            var control = AvaloniaRuntimeXamlLoader.Parse <UserControl>(xaml);

            var panel = control.FindControl <Panel>("panel");

            Assert.Equal(2, panel.Children.Count);

            var foo = control.FindControl <ContentControl>("Foo");
            var bar = control.FindControl <ContentControl>("Bar");

            Assert.Contains(foo, panel.Children);
            Assert.Contains(bar, panel.Children);
        }
Пример #9
0
        public void Control_Is_Added_To_Parent_Before_Final_EndInit()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <local:InitializationOrderTracker Width='100'/>
</Window>";

                var window  = AvaloniaRuntimeXamlLoader.Parse <Window>(xaml);
                var tracker = (InitializationOrderTracker)window.Content;

                var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
                var endInit  = tracker.Order.IndexOf("EndInit 0");

                Assert.NotEqual(-1, attached);
                Assert.NotEqual(-1, endInit);
                Assert.True(attached < endInit);
            }
        }
Пример #10
0
        public void ControlTemplate_With_Panel_Children_Are_Added()
        {
            var xaml     = @"
<ControlTemplate xmlns='https://github.com/avaloniaui'>
    <Panel Name='panel'>
        <ContentControl Name='Foo' />
        <ContentControl Name='Bar' />
    </Panel>
</ControlTemplate>
";
            var template = AvaloniaRuntimeXamlLoader.Parse <ControlTemplate>(xaml);

            var panel = (Panel)template.Build(new ContentControl()).Control;

            Assert.Equal(2, panel.Children.Count);

            var foo = panel.Children[0];
            var bar = panel.Children[1];

            Assert.Equal("Foo", foo.Name);
            Assert.Equal("Bar", bar.Name);
        }
Пример #11
0
        /// <summary>
        /// <inheritdoc cref="IDynamicViewPresenter.GetView(IDynamicViewModel)"/>
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        public IView GetView(IDynamicViewModel vm)
        {
            string template = vm.Template;

            logger.Debug($"Try to parse template {template}");

            // Get template, check if template is not null.
            if (!File.Exists(template))
            {
                logger.Error("Template does not exist is null");
                return(null);
            }

            // Render xaml content
            string xaml = templateEngine.Render(template, vm.DataSource);

            try
            {
                // https://github.com/verybadcat/CSharpMath/pull/149
                // moved into the separate package: Avalonia.Markup.Xaml.Loader
                // dynamically load xaml
                object view = AvaloniaRuntimeXamlLoader.Parse(xaml);

                if (view is IView control)
                {
                    // binding datacontext
                    control.DataContext = vm;
                    return(control);
                }

                logger.Error($"Dynamic view must be a instance of {typeof(DynamicViewBase).FullName}");
            }
            catch (Exception e)
            {
                logger.Error($"Could not parse xaml template dynamically, {e.StackTrace}");
            }
            return(null);
        }
Пример #12
0
        public void Grid_Row_Col_Definitions_Are_Built()
        {
            var xaml = @"
<Grid xmlns='https://github.com/avaloniaui'>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width='100' />
        <ColumnDefinition Width='Auto' />
        <ColumnDefinition Width='*' />
        <ColumnDefinition Width='100*' />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height='100' />
        <RowDefinition Height='Auto' />
        <RowDefinition Height='*' />
        <RowDefinition Height='100*' />
    </Grid.RowDefinitions>
</Grid>";

            var grid = AvaloniaRuntimeXamlLoader.Parse <Grid>(xaml);

            Assert.Equal(4, grid.ColumnDefinitions.Count);
            Assert.Equal(4, grid.RowDefinitions.Count);

            var expected1 = new GridLength(100);
            var expected2 = GridLength.Auto;
            var expected3 = new GridLength(1, GridUnitType.Star);
            var expected4 = new GridLength(100, GridUnitType.Star);

            Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
            Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
            Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
            Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);

            Assert.Equal(expected1, grid.RowDefinitions[0].Height);
            Assert.Equal(expected2, grid.RowDefinitions[1].Height);
            Assert.Equal(expected3, grid.RowDefinitions[2].Height);
            Assert.Equal(expected4, grid.RowDefinitions[3].Height);
        }
Пример #13
0
        public void StyleInclude_Is_Built()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Styles xmlns='https://github.com/avaloniaui'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <StyleInclude Source='avares://Avalonia.Themes.Default/Controls/ContextMenu.xaml'/>
</Styles>";

                var styles = AvaloniaRuntimeXamlLoader.Parse <Styles>(xaml);

                Assert.True(styles.Count == 1);

                var styleInclude = styles.First() as StyleInclude;

                Assert.NotNull(styleInclude);

                var style = styleInclude.Loaded;

                Assert.NotNull(style);
            }
        }
Пример #14
0
        public void Style_Setter_With_AttachedProperty_Is_Parsed()
        {
            var xaml = @"
<Styles xmlns='https://github.com/avaloniaui'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Style Selector='ContentControl'>
        <Setter Property='TextBlock.FontSize' Value='21'/>
    </Style>
</Styles>";

            var styles = AvaloniaRuntimeXamlLoader.Parse <Styles>(xaml);

            Assert.Single(styles);

            var style = (Style)styles[0];

            var setters = style.Setters.Cast <Setter>().ToArray();

            Assert.Single(setters);

            Assert.Equal(TextBlock.FontSizeProperty, setters[0].Property);
            Assert.Equal(21.0, setters[0].Value);
        }
Пример #15
0
        public void StyleInclude_Is_Built()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow
                                             .With(theme: () => new Styles())))
            {
                var xaml = @"
<ContentControl xmlns='https://github.com/avaloniaui'>
    <ContentControl.Styles>
        <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
    </ContentControl.Styles>
</ContentControl>";

                var window = AvaloniaRuntimeXamlLoader.Parse <ContentControl>(xaml);

                Assert.Single(window.Styles);

                var styleInclude = window.Styles[0] as StyleInclude;

                Assert.NotNull(styleInclude);
                Assert.NotNull(styleInclude.Source);
                Assert.NotNull(styleInclude.Loaded);
            }
        }
Пример #16
0
        public void Binding_To_List_AvaloniaProperty_Is_Operational()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'>
    <ListBox Items='{Binding Items}' SelectedItems='{Binding SelectedItems}'/>
</Window>";

                var window  = AvaloniaRuntimeXamlLoader.Parse <Window>(xaml);
                var listBox = (ListBox)window.Content;

                var vm = new SelectedItemsViewModel()
                {
                    Items = new string[] { "foo", "bar", "baz" }
                };

                window.DataContext = vm;

                Assert.Equal(vm.Items, listBox.Items);

                Assert.Equal(vm.SelectedItems, listBox.SelectedItems);
            }
        }
Пример #17
0
        public void Attached_Properties_From_Static_Types_Should_Work_In_Style_Setters_Bug_2561()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var parsed = (Window)AvaloniaRuntimeXamlLoader.Parse(@"
<Window
  xmlns='https://github.com/avaloniaui'
  xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests;assembly=Avalonia.Markup.Xaml.UnitTests'
>
  <Window.Styles>
    <Style Selector='TextBox'>
      <Setter Property='local:XamlIlBugTestsStaticClassWithAttachedProperty.TestInt' Value='100'/>
    </Style>
  </Window.Styles>
  <TextBox/>

</Window>
");
                var tb     = ((TextBox)parsed.Content);
                parsed.Show();
                tb.ApplyTemplate();
                Assert.Equal(100, XamlIlBugTestsStaticClassWithAttachedProperty.GetTestInt(tb));
            }
        }
Пример #18
0
        public void Direct_Content_In_ItemsControl_Is_Operational()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'>
     <ItemsControl Name='items'>
         <ContentControl>Foo</ContentControl>
         <ContentControl>Bar</ContentControl>
      </ItemsControl>
</Window>";

                var control = AvaloniaRuntimeXamlLoader.Parse <Window>(xaml);

                var itemsControl = control.FindControl <ItemsControl>("items");

                Assert.NotNull(itemsControl);

                var items = itemsControl.Items.Cast <ContentControl>().ToArray();

                Assert.Equal("Foo", items[0].Content);
                Assert.Equal("Bar", items[1].Content);
            }
        }
Пример #19
0
        private void ApplyConfiguredTheme()
        {
            try
            {
                string baseStyle         = ConfigurationState.Instance.Ui.BaseStyle;
                string themePath         = ConfigurationState.Instance.Ui.CustomThemePath;
                bool   enableCustomTheme = ConfigurationState.Instance.Ui.EnableCustomTheme;

                const string BaseStyleUrl = "avares://Ryujinx.Ava/Assets/Styles/Base{0}.xaml";

                if (string.IsNullOrWhiteSpace(baseStyle))
                {
                    ConfigurationState.Instance.Ui.BaseStyle.Value = "Dark";

                    baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
                }

                var theme = AvaloniaLocator.Current.GetService <FluentAvaloniaTheme>();

                theme.RequestedTheme = baseStyle;

                var currentStyles = this.Styles;

                // Remove all styles except the base style.
                if (currentStyles.Count > 1)
                {
                    currentStyles.RemoveRange(1, currentStyles.Count - 1);
                }

                IStyle newStyles = null;

                // Load requested style, and fallback to Dark theme if loading failed.
                try
                {
                    newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, baseStyle), UriKind.Absolute));
                }
                catch (XamlLoadException)
                {
                    newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, "Dark"), UriKind.Absolute));
                }

                currentStyles.Add(newStyles);

                if (enableCustomTheme)
                {
                    if (!string.IsNullOrWhiteSpace(themePath))
                    {
                        try
                        {
                            var themeContent = File.ReadAllText(themePath);
                            var customStyle  = AvaloniaRuntimeXamlLoader.Parse <IStyle>(themeContent);

                            currentStyles.Add(customStyle);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error?.Print(LogClass.Application, $"Failed to Apply Custom Theme. Error: {ex.Message}");
                        }
                    }
                }
            }
            catch (Exception)
            {
                Logger.Warning?.Print(LogClass.Application, "Failed to Apply Theme. A restart is needed to apply the selected theme");

                ShowRestartDialog();
            }
        }
Пример #20
0
        private Styles LoadStylesFrom(string xamlPath)
        {
            string styleXaml = File.ReadAllText(xamlPath);

            return(AvaloniaRuntimeXamlLoader.Parse <Styles>(styleXaml));
        }