From 52d122eb132e15a1d8ba1583d458487b407b4e68 Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Tue, 25 Oct 2022 22:28:45 +0200 Subject: [PATCH] Gotten some files back, in the end they were referenced from the xaml --- .../Controls/Emoji/ChunkHelper.cs | 69 +++++++ .../Controls/Emoji/EmojiPicker.xaml | 2 +- .../Controls/Emoji/EmojiPicker.xaml.cs | 181 +++++++++--------- src/Greenshot.Editor/Controls/Emoji/Emojis.cs | 4 + .../Drawing/Emoji/EmojiContainer.cs | 12 +- 5 files changed, 176 insertions(+), 92 deletions(-) create mode 100644 src/Greenshot.Editor/Controls/Emoji/ChunkHelper.cs diff --git a/src/Greenshot.Editor/Controls/Emoji/ChunkHelper.cs b/src/Greenshot.Editor/Controls/Emoji/ChunkHelper.cs new file mode 100644 index 000000000..d4aa24002 --- /dev/null +++ b/src/Greenshot.Editor/Controls/Emoji/ChunkHelper.cs @@ -0,0 +1,69 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: https://getgreenshot.org/ + * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Greenshot.Editor.Controls.Emoji; + +/// +/// This seems to enumerate multiple IEnumerables. +/// TODO: Replace this with LINQ +/// +/// +internal sealed class ChunkHelper : IEnumerable> +{ + private readonly IEnumerable _elements; + private readonly int _size; + private bool _hasMore; + + public ChunkHelper(IEnumerable elements, int size) + { + _elements = elements; + _size = size; + } + + public IEnumerator> GetEnumerator() + { + using var enumerator = _elements.GetEnumerator(); + _hasMore = enumerator.MoveNext(); + while (_hasMore) + { + yield return GetNextBatch(enumerator).ToList(); + } + } + + private IEnumerable GetNextBatch(IEnumerator enumerator) + { + for (int i = 0; i < _size; ++i) + { + yield return enumerator.Current; + _hasMore = enumerator.MoveNext(); + if (!_hasMore) + { + yield break; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} \ No newline at end of file diff --git a/src/Greenshot.Editor/Controls/Emoji/EmojiPicker.xaml b/src/Greenshot.Editor/Controls/Emoji/EmojiPicker.xaml index ddbb78b2d..349515ce1 100644 --- a/src/Greenshot.Editor/Controls/Emoji/EmojiPicker.xaml +++ b/src/Greenshot.Editor/Controls/Emoji/EmojiPicker.xaml @@ -1,4 +1,4 @@ - +/// The event which is created when the emoji is picked +/// +public class EmojiPickedEventArgs : EventArgs { - /// - /// The event which is created when the emoji is picked - /// - public class EmojiPickedEventArgs : EventArgs - { - public EmojiPickedEventArgs() { } - public EmojiPickedEventArgs(string emoji) => Emoji = emoji; + public EmojiPickedEventArgs() { } + public EmojiPickedEventArgs(string emoji) => Emoji = emoji; - public string Emoji; + public string Emoji; +} + +public delegate void EmojiPickedEventHandler(object sender, EmojiPickedEventArgs e); + +/// +/// Interaction logic for Picker.xaml +/// +public partial class EmojiPicker : StackPanel +{ + public EmojiPicker() + { + InitializeComponent(); } - public delegate void EmojiPickedEventHandler(object sender, EmojiPickedEventArgs e); + public IList EmojiGroups => EmojiData.Data.Groups; - /// - /// Interaction logic for Picker.xaml - /// - public partial class EmojiPicker : StackPanel + // Backwards compatibility for when the backend was a TextBlock. + public double FontSize { - public EmojiPicker() + get => Image.Height * 0.75; + set => Image.Height = value / 0.75; + } + + public event PropertyChangedEventHandler SelectionChanged; + + public event EmojiPickedEventHandler Picked; + + private static void OnSelectionPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) + { + (source as EmojiPicker)?.OnSelectionChanged(e.NewValue as string); + } + + public string Selection + { + get => (string)GetValue(SelectionProperty); + set => SetValue(SelectionProperty, value); + } + + private void OnSelectionChanged(string s) + { + var isDisabled = string.IsNullOrEmpty(s); + Image.Emoji = isDisabled ? "???" : s; + Image.Opacity = isDisabled ? 0.3 : 1.0; + SelectionChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Selection))); + } + + private void OnEmojiPicked(object sender, RoutedEventArgs e) + { + if (sender is not Control { DataContext: Emojis.Emoji emoji }) { - InitializeComponent(); + return; } - public IList EmojiGroups => EmojiData.Data.Groups; - - // Backwards compatibility for when the backend was a TextBlock. - public double FontSize + if (emoji.Variations.Count != 0 && sender is not Button) { - get => Image.Height * 0.75; - set => Image.Height = value / 0.75; + return; } - public event PropertyChangedEventHandler SelectionChanged; + Selection = emoji.Text; + Button_INTERNAL.IsChecked = false; + e.Handled = true; + Picked?.Invoke(this, new EmojiPickedEventArgs(Selection)); + } - public event EmojiPickedEventHandler Picked; + public static readonly DependencyProperty SelectionProperty = DependencyProperty.Register( + nameof(Selection), typeof(string), typeof(EmojiPicker), + new FrameworkPropertyMetadata("☺", OnSelectionPropertyChanged)); - private static void OnSelectionPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) + private void OnPopupKeyDown(object sender, KeyEventArgs e) + { + if (e.Key != Key.Escape || sender is not Popup popup) return; + popup.IsOpen = false; + e.Handled = true; + } + + private void OnPopupLoaded(object sender, RoutedEventArgs e) + { + if (sender is not Popup popup) return; + + var child = popup.Child; + IInputElement oldFocus = null; + child.Focusable = true; + child.IsVisibleChanged += (o, ea) => { - (source as EmojiPicker)?.OnSelectionChanged(e.NewValue as string); - } + if (!child.IsVisible) return; + oldFocus = Keyboard.FocusedElement; + Keyboard.Focus(child); + }; - public string Selection + popup.Closed += (o, ea) => Keyboard.Focus(oldFocus); + } + + public void ShowPopup(bool show) + { + foreach (var child in Children) { - get => (string)GetValue(SelectionProperty); - set => SetValue(SelectionProperty, value); - } - - private void OnSelectionChanged(string s) - { - var isDisabled = string.IsNullOrEmpty(s); - Image.Emoji = isDisabled ? "???" : s; - Image.Opacity = isDisabled ? 0.3 : 1.0; - SelectionChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Selection))); - } - - private void OnEmojiPicked(object sender, RoutedEventArgs e) - { - if (sender is not Control { DataContext: Emojis.Emoji emoji }) return; - if (emoji.Variations.Count != 0 && sender is not Button) return; - - Selection = emoji.Text; - Button_INTERNAL.IsChecked = false; - e.Handled = true; - Picked?.Invoke(this, new EmojiPickedEventArgs(Selection)); - } - - public static readonly DependencyProperty SelectionProperty = DependencyProperty.Register( - nameof(Selection), typeof(string), typeof(EmojiPicker), - new FrameworkPropertyMetadata("☺", OnSelectionPropertyChanged)); - - private void OnPopupKeyDown(object sender, KeyEventArgs e) - { - if (e.Key != Key.Escape || sender is not Popup popup) return; - popup.IsOpen = false; - e.Handled = true; - } - - private void OnPopupLoaded(object sender, RoutedEventArgs e) - { - if (sender is not Popup popup) return; - - var child = popup.Child; - IInputElement oldFocus = null; - child.Focusable = true; - child.IsVisibleChanged += (o, ea) => + if (child is ToggleButton button) { - if (!child.IsVisible) return; - oldFocus = Keyboard.FocusedElement; - Keyboard.Focus(child); - }; - - popup.Closed += (o, ea) => Keyboard.Focus(oldFocus); - } - - public void ShowPopup(bool show) - { - foreach (var child in Children) - { - if (child is ToggleButton button) - { - button.IsChecked = show; - } + button.IsChecked = show; } } } -} +} \ No newline at end of file diff --git a/src/Greenshot.Editor/Controls/Emoji/Emojis.cs b/src/Greenshot.Editor/Controls/Emoji/Emojis.cs index ff1c74971..95b8a2f8b 100644 --- a/src/Greenshot.Editor/Controls/Emoji/Emojis.cs +++ b/src/Greenshot.Editor/Controls/Emoji/Emojis.cs @@ -42,7 +42,11 @@ public class Emojis [XmlElement(ElementName = "Emoji")] public List Emojis { get; set; } = new(); + public IEnumerable> EmojiChunkList => new ChunkHelper(EmojiList, 8); + public string Icon => SubGroups.FirstOrDefault()?.Emojis.FirstOrDefault()?.Text; + + public IEnumerable EmojiList => SubGroups.SelectMany(s => s.Emojis); } public class Emoji diff --git a/src/Greenshot.Editor/Drawing/Emoji/EmojiContainer.cs b/src/Greenshot.Editor/Drawing/Emoji/EmojiContainer.cs index ecd6202b3..6d91b8ddb 100644 --- a/src/Greenshot.Editor/Drawing/Emoji/EmojiContainer.cs +++ b/src/Greenshot.Editor/Drawing/Emoji/EmojiContainer.cs @@ -26,7 +26,7 @@ using System.Windows.Forms; using System.Windows.Forms.Integration; using Greenshot.Base.Core; using Greenshot.Base.Interfaces.Drawing; -using Greenshot.Editor.Controls; +using Greenshot.Editor.Controls.Emoji; using Greenshot.Editor.Helpers; using Image = System.Drawing.Image; @@ -95,7 +95,10 @@ namespace Greenshot.Editor.Drawing.Emoji // Create one picker control by surface // TODO: This is not ideal, as we need to controls from the surface, should replace this with a different solution. _emojiPickerHost = _parent.Controls.Find("EmojiPickerHost", false).OfType().FirstOrDefault(); - if (_emojiPickerHost != null) return; + if (_emojiPickerHost != null) + { + return; + } _emojiPicker = new EmojiPicker(); _emojiPicker.Picked += (_, args) => @@ -136,7 +139,10 @@ namespace Greenshot.Editor.Drawing.Emoji /// PropertyChangedEventArgs private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (!e.PropertyName.Equals(nameof(Selected))) return; + if (!e.PropertyName.Equals(nameof(Selected))) + { + return; + } if (!Selected) {