Ejemplo n.º 1
0
            Size _Measure()
            {
                _dpi = Dpi.OfWindow(_w);
                int i = _cellSize = Dpi.Scale(10, _dpi);
                var z = new SIZE(i, i);

                z.width  *= c_nHue; z.width++;
                z.height *= c_nLum + 1; z.height++; z.height += _cellSize / 4;
                return(Dpi.Unscale(z, _dpi));
            }
Ejemplo n.º 2
0
Archivo: DIcons.cs Proyecto: qmgindi/Au
    DIcons(bool expandFileIcon, bool randomizeColors)
    {
        Title         = "Icons";
        Owner         = App.Wmain;
        ShowInTaskbar = false;

        var b = new wpfBuilder(this).WinSize(600, 600);

        b.Columns(-1, 0);

        //left - edit control and tree view
        b.Row(-1).StartDock();
        b.Add(out _tName).Tooltip(@"Search.
Part of icon name, or wildcard expression.
Examples: part, Part (match case), start*, *end, **rc regex case-sensitive.
Can be Pack.Icon, like Modern.List.")
        .Dock(Dock.Top).Focus();
        //b.Focus(); //currently cannot use this because of WPF tooltip bugs
        b.xAddInBorder(out KTreeView tv);         //tv.SingleClickActivate = true;
        b.End();

        //right - color picker, buttons, etc
        b.StartGrid().Columns(-1);
        b.Add(out KColorPicker colors);
        colors.ColorChanged += color => {
            _random = null;
            _color  = _ColorToString(color);
            tv.Redraw();
        };
        b.StartStack();

        TextBox randFromTo = null, iconSizes = null;

        b.AddButton("Randomize colors", _ => _RandomizeColors());
        b.Add("L %", out randFromTo, "30-70").Width(50);
        b.End();
        b.AddSeparator().Margin("B20");

        //rejected: double-clicking an icon clicks the last clicked button. Unclear and not so useful.
        //_Action lastAction = 0;
        tv.ItemActivated += (o, e) => {
            //	switch (lastAction) {
            //	case 0: break;
            //	case _Action.FileIcon: _SetIcon(tv); break;
            //	default: _InsertCodeOrExport(tv, lastAction); break;
            //	}
            _InsertCodeOrExport(tv, _Action.MenuIcon);
        };

        b.StartStack <Expander>(out var exp1, "Set icon of selected files");
        b.AddButton(out var bThis, "This", _ => { _SetIcon(tv); /*lastAction = _Action.FileIcon;*/ }).Width(70).Disabled();
        b.AddButton("Default", _ => _SetIcon(null)).Width(70);
        //b.AddButton("Random", null).Width(70); //idea: set random icons for multiple selected files. Probably too crazy.
        b.AddButton("Show current", _ => _tName.Text = FilesModel.TreeControl.SelectedItems.FirstOrDefault()?.CustomIconName).Margin("L20");
        b.End();
        if (expandFileIcon)
        {
            exp1.IsExpanded = true;
        }

        b.StartGrid <Expander>("Insert code for menu/toolbar/etc icon");
        b.R.Add <Label>("Set icon of: ");
        b.StartStack();
        b.AddButton(out var bMenuItem, "Menu or toolbar item", _ => _InsertCodeOrExport(tv, _Action.MenuIcon)).Disabled()
        .Tooltip("To assign the selected icon to a toolbar button or menu item,\nin the code editor click its line (anywhere except action code)\nand then click this button. Or double-click an icon.");
        b.End();
        b.R.Add <Label>("Insert line: ");
        b.StartStack();
        b.AddButton(out var bCodeVar, "Variable = XAML", _ => _InsertCodeOrExport(tv, _Action.InsertXamlVar)).Disabled();
        b.AddButton(out var bCodeField, "Field = XAML", _ => _InsertCodeOrExport(tv, _Action.InsertXamlField)).Disabled();
        b.End();
        b.R.Add <Label>("Copy text: ");
        b.StartStack();
        b.AddButton(out var bCodeName, "Name", _ => _InsertCodeOrExport(tv, _Action.CopyName)).Width(70).Disabled()
        .Tooltip("Shorter string than XAML.\nCan be used with custom menus and toolbars,\neditor menus and toolbars (edit Commands.xml),\nscript.editor.GetIcon, IconImageCache, ImageUtil,\noutput tag <image>.");
        b.AddButton(out var bCodeXaml, "XAML", _ => _InsertCodeOrExport(tv, _Action.CopyXaml)).Width(70).Disabled();
        b.End();
        //b.Add<Label>("Tip: double-clicking an icon clicks the same button.");
        b.End();

        b.StartStack <Expander>("Export to current workspace folder");
        b.AddButton(out var bExportXaml, ".xaml", _ => _InsertCodeOrExport(tv, _Action.ExportXaml)).Width(70).Disabled();
        b.AddButton(out var bExportIco, ".ico", _ => _InsertCodeOrExport(tv, _Action.ExportIcon)).Width(70).Disabled();
        b.Add("sizes", out iconSizes, "16,24,32,48,64").Width(100);
        b.End();

        b.StartStack <Expander>("Other actions");
        b.AddButton("Clear program's icon cache", _ => IconImageCache.Common.Clear(redrawWindows: true));
        //SHOULDDO: clear when app version changes.
        b.End();

        //b.StartGrid<Expander>("List display options");
        ////b.Add("Background", out ComboBox cBackground).Items("Default|Control|White|Black)");
        ////cBackground.SelectionChanged += (o, e) => _ChangeBackground();
        //b.Add(out KCheckBox cCollection, "Collection");
        //cCollection.CheckChanged += (_, _) => {
        //	_withCollection = cCollection.IsChecked == true;
        //	tv.Redraw();
        //};
        //b.End();

        b.Row(-1);
        b.R.Add <TextBlock>().Align("R").Text("Thanks to ", "<a>MahApps.Metro.IconPacks", new Action(() => run.it("https://github.com/MahApps/MahApps.Metro.IconPacks")));
        b.End();

        b.End();

        b.Loaded += () => {
            _dpi = Dpi.OfWindow(this);
            _OpenDB();

            _a = new(30000);
            foreach (var(table, _) in s_tables)
            {
                using var stat = s_db.Statement("SELECT name FROM " + table);
                while (stat.Step())
                {
                    var k = new _Item(table, stat.GetText(0));
                    //var s = _ColorName(k); if (s.Length < 20 || s.Length > 60) print.it(s.Length, s);
                    _a.Add(k);
                }
            }
            _a.Sort((a, b) => string.Compare(a._name, b._name, StringComparison.OrdinalIgnoreCase));
            if (randomizeColors)
            {
                _RandomizeColors();
            }
            tv.SetItems(_a);
        };

        _tName.TextChanged += (_, _) => {
            string             name = _tName.Text, table = null;
            Func <_Item, bool> f = null;
            bool select          = false;
            if (!name.NE())
            {
                if (select = name.RxMatch(@"^\*(\w+)\.(\w+) #(\w+)$", out var m))                   //full name with * and #color
                {
                    table        = m[1].Value;
                    name         = m[2].Value;
                    f            = o => o._name == name && o._table == table;
                    colors.Color = m[3].Value.ToInt(0, STIFlags.IsHexWithout0x);
                }
                else
                {
                    if (name.RxMatch(@"^(\w+)\.(.+)", out m))
                    {
                        table = m[1].Value; name = m[2].Value;
                    }
                    wildex           wild      = null;
                    StringComparison comp      = StringComparison.OrdinalIgnoreCase;
                    bool             matchCase = name.RxIsMatch("[A-Z]");
                    if (wildex.hasWildcardChars(name))
                    {
                        try { wild = new wildex(name, matchCase && !name.Starts("**")); }
                        catch { name = null; }
                    }
                    else if (matchCase)
                    {
                        comp = StringComparison.Ordinal;
                    }

                    if (name != null)
                    {
                        f = o => (table == null || o._table.Eqi(table)) && (wild?.Match(o._name) ?? o._name.Contains(name, comp));
                    }
                }
            }
            var e = f == null ? _a : _a.Where(f);
            tv.SetItems(e);
            if (select && (select = e.Count() == 1))
            {
                tv.Select(0);
            }
            _EnableControls(select);
        };

        tv.SelectedSingle += (o, i) => {
            _EnableControls(true);
            //var k = _a[i];
            //if(GetIconFromBigDB(k._table, k._name, _ItemColor(k), out var xaml)) {
            //	print.it(xaml);
            //}
        };

        b.WinSaved(App.Settings.wndpos.icons, o => App.Settings.wndpos.icons = o);

        void _EnableControls(bool enable)
        {
            bThis.IsEnabled       = enable;
            bMenuItem.IsEnabled   = enable;
            bCodeVar.IsEnabled    = enable;
            bCodeField.IsEnabled  = enable;
            bCodeXaml.IsEnabled   = enable;
            bCodeName.IsEnabled   = enable;
            bExportXaml.IsEnabled = enable;
            bExportIco.IsEnabled  = enable;
        }

        void _SetIcon(KTreeView tv)
        {
            string icon = tv?.SelectedItem is _Item k?_ColorName(k) : null;

            foreach (var v in FilesModel.TreeControl.SelectedItems)
            {
                v.CustomIconName = icon;
            }
        }

        void _InsertCodeOrExport(KTreeView tv, _Action what)
        {
            //lastAction = what;
            if (tv.SelectedItem is not _Item k)
            {
                return;
            }
            string code = null;

            if (what == _Action.MenuIcon)
            {
                InsertCode.SetMenuToolbarItemIcon(_ColorName(k));
            }
            else if (what == _Action.CopyName)
            {
                code = _ColorName(k);
            }
            else if (GetIconFromBigDB(k._table, k._name, _ItemColor(k), out var xaml))
            {
                xaml = xaml.Replace('\"', '\'').RxReplace(@"\R\s*", "");
                switch (what)
                {
                case _Action.InsertXamlVar: code = $"string icon{k._name} = \"{xaml}\";"; break;

                case _Action.InsertXamlField: code = $"public const string {k._name} = \"{xaml}\";"; break;

                case _Action.CopyXaml: code = xaml; break;

                case _Action.ExportXaml: _Export(false); break;

                case _Action.ExportIcon: _Export(true); break;
                }

                void _Export(bool ico)
                {
                    //App.Model.New
                    var cf = App.Model.CurrentFile.Parent; if (cf == null)
                    {
                        return;
                    }
                    var path = $"{cf.FilePath}\\{k._name}{(ico ? ".ico" : ".xaml")}";

                    //CONSIDER: if path exists, show dialog
                    if (ico)
                    {
                        var sizes = iconSizes.Text.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(o => o.ToInt()).ToArray();
                        KImageUtil.XamlImageToIconFile(path, xaml, sizes);
                    }
                    else
                    {
                        filesystem.saveText(path, xaml);
                    }
                    var fn = App.Model.ImportFromWorkspaceFolder(path, cf, FNPosition.Inside);

                    if (fn == null)
                    {
                        print.it("failed");
                    }
                    else
                    {
                        print.it($"<>Icon exported to <open>{fn.ItemPath}<>");
                    }
                }
            }

            if (code != null)
            {
                if (what is _Action.CopyName or _Action.CopyXaml)
                {
                    clipboard.text = code;
                }
                else if (what is _Action.InsertXamlVar or _Action.InsertXamlField)
                {
                    InsertCode.Statements(code);
                }