|
| 1 | +using System.Windows; |
| 2 | +using System.Windows.Controls; |
| 3 | +using System.Windows.Media; |
| 4 | + |
| 5 | +namespace Flow.Launcher.Resources.Controls |
| 6 | +{ |
| 7 | + public partial class InfoBar : UserControl |
| 8 | + { |
| 9 | + public InfoBar() |
| 10 | + { |
| 11 | + InitializeComponent(); |
| 12 | + Loaded += InfoBar_Loaded; |
| 13 | + } |
| 14 | + |
| 15 | + private void InfoBar_Loaded(object sender, RoutedEventArgs e) |
| 16 | + { |
| 17 | + UpdateStyle(); |
| 18 | + UpdateTitleVisibility(); |
| 19 | + UpdateMessageVisibility(); |
| 20 | + UpdateOrientation(); |
| 21 | + UpdateIconAlignmentAndMargin(); |
| 22 | + |
| 23 | + // DataContext 설정 (예시) |
| 24 | + this.DataContext = this; // InfoBar 자체를 DataContext로 사용 |
| 25 | + } |
| 26 | + |
| 27 | + public static readonly DependencyProperty TypeProperty = |
| 28 | + DependencyProperty.Register(nameof(Type), typeof(InfoBarType), typeof(InfoBar), new PropertyMetadata(InfoBarType.Info, OnTypeChanged)); |
| 29 | + |
| 30 | + public InfoBarType Type |
| 31 | + { |
| 32 | + get => (InfoBarType)GetValue(TypeProperty); |
| 33 | + set => SetValue(TypeProperty, value); |
| 34 | + } |
| 35 | + |
| 36 | + private static void OnTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 37 | + { |
| 38 | + if (d is InfoBar infoBar) |
| 39 | + { |
| 40 | + infoBar.UpdateStyle(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static readonly DependencyProperty MessageProperty = |
| 45 | + DependencyProperty.Register(nameof(Message), typeof(string), typeof(InfoBar), new PropertyMetadata(string.Empty, OnMessageChanged)); |
| 46 | + |
| 47 | + public string Message |
| 48 | + { |
| 49 | + get => (string)GetValue(MessageProperty); |
| 50 | + set |
| 51 | + { |
| 52 | + SetValue(MessageProperty, value); |
| 53 | + UpdateMessageVisibility(); // Message 속성 변경 시 Visibility 업데이트 |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 58 | + { |
| 59 | + if (d is InfoBar infoBar) |
| 60 | + { |
| 61 | + infoBar.UpdateMessageVisibility(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void UpdateMessageVisibility() |
| 66 | + { |
| 67 | + PART_Message.Visibility = string.IsNullOrEmpty(Message) ? Visibility.Collapsed : Visibility.Visible; |
| 68 | + } |
| 69 | + |
| 70 | + public static readonly DependencyProperty TitleProperty = |
| 71 | + DependencyProperty.Register(nameof(Title), typeof(string), typeof(InfoBar), new PropertyMetadata(string.Empty, OnTitleChanged)); |
| 72 | + |
| 73 | + public string Title |
| 74 | + { |
| 75 | + get => (string)GetValue(TitleProperty); |
| 76 | + set |
| 77 | + { |
| 78 | + SetValue(TitleProperty, value); |
| 79 | + UpdateTitleVisibility(); // Title 속성 변경 시 Visibility 업데이트 |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 84 | + { |
| 85 | + if (d is InfoBar infoBar) |
| 86 | + { |
| 87 | + infoBar.UpdateTitleVisibility(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void UpdateTitleVisibility() |
| 92 | + { |
| 93 | + PART_Title.Visibility = string.IsNullOrEmpty(Title) ? Visibility.Collapsed : Visibility.Visible; |
| 94 | + } |
| 95 | + |
| 96 | + public static readonly DependencyProperty IsIconVisibleProperty = |
| 97 | + DependencyProperty.Register(nameof(IsIconVisible), typeof(bool), typeof(InfoBar), new PropertyMetadata(true, OnIsIconVisibleChanged)); |
| 98 | + |
| 99 | + public bool IsIconVisible |
| 100 | + { |
| 101 | + get => (bool)GetValue(IsIconVisibleProperty); |
| 102 | + set => SetValue(IsIconVisibleProperty, value); |
| 103 | + } |
| 104 | + |
| 105 | + public static readonly DependencyProperty LengthProperty = |
| 106 | + DependencyProperty.Register(nameof(Length), typeof(InfoBarLength), typeof(InfoBar), new PropertyMetadata(InfoBarLength.Short, OnLengthChanged)); |
| 107 | + |
| 108 | + public InfoBarLength Length |
| 109 | + { |
| 110 | + get { return (InfoBarLength)GetValue(LengthProperty); } |
| 111 | + set { SetValue(LengthProperty, value); } |
| 112 | + } |
| 113 | + |
| 114 | + private static void OnLengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 115 | + { |
| 116 | + if (d is InfoBar infoBar) |
| 117 | + { |
| 118 | + infoBar.UpdateOrientation(); |
| 119 | + infoBar.UpdateIconAlignmentAndMargin(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void UpdateOrientation() |
| 124 | + { |
| 125 | + PART_StackPanel.Orientation = Length == InfoBarLength.Long ? Orientation.Vertical : Orientation.Horizontal; |
| 126 | + } |
| 127 | + |
| 128 | + private void UpdateIconAlignmentAndMargin() |
| 129 | + { |
| 130 | + if (Length == InfoBarLength.Short) |
| 131 | + { |
| 132 | + Part_IconBorder.VerticalAlignment = VerticalAlignment.Center; |
| 133 | + Part_IconBorder.Margin = new Thickness(0, 0, 12, 0); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + Part_IconBorder.VerticalAlignment = VerticalAlignment.Top; |
| 138 | + Part_IconBorder.Margin = new Thickness(0, 2, 12, 0); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static readonly DependencyProperty ClosableProperty = |
| 143 | + DependencyProperty.Register(nameof(Closable), typeof(bool), typeof(InfoBar), new PropertyMetadata(true, OnClosableChanged)); |
| 144 | + |
| 145 | + public bool Closable |
| 146 | + { |
| 147 | + get => (bool)GetValue(ClosableProperty); |
| 148 | + set => SetValue(ClosableProperty, value); |
| 149 | + } |
| 150 | + |
| 151 | + private void PART_CloseButton_Click(object sender, RoutedEventArgs e) |
| 152 | + { |
| 153 | + Visibility = Visibility.Collapsed; |
| 154 | + } |
| 155 | + |
| 156 | + private void UpdateStyle() |
| 157 | + { |
| 158 | + switch (Type) |
| 159 | + { |
| 160 | + case InfoBarType.Info: |
| 161 | + PART_Border.Background = (Brush)FindResource("InfoBarInfoBG"); |
| 162 | + Part_IconBorder.Background = (Brush)FindResource("InfoBarInfoIcon"); |
| 163 | + PART_Icon.Glyph = "\xF13F"; |
| 164 | + break; |
| 165 | + case InfoBarType.Success: |
| 166 | + PART_Border.Background = (Brush)FindResource("InfoBarSuccessBG"); |
| 167 | + Part_IconBorder.Background = (Brush)FindResource("InfoBarSuccessIcon"); |
| 168 | + PART_Icon.Glyph = "\xF13E"; |
| 169 | + break; |
| 170 | + case InfoBarType.Warning: |
| 171 | + PART_Border.Background = (Brush)FindResource("InfoBarWarningBG"); |
| 172 | + Part_IconBorder.Background = (Brush)FindResource("InfoBarWarningIcon"); |
| 173 | + PART_Icon.Glyph = "\xF13C"; |
| 174 | + break; |
| 175 | + case InfoBarType.Error: |
| 176 | + PART_Border.Background = (Brush)FindResource("InfoBarErrorBG"); |
| 177 | + Part_IconBorder.Background = (Brush)FindResource("InfoBarErrorIcon"); |
| 178 | + PART_Icon.Glyph = "\xF13D"; |
| 179 | + break; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static void OnIsIconVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 184 | + { |
| 185 | + var infoBar = (InfoBar)d; |
| 186 | + infoBar.UpdateIconVisibility(); |
| 187 | + } |
| 188 | + |
| 189 | + private static void OnClosableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 190 | + { |
| 191 | + var infoBar = (InfoBar)d; |
| 192 | + infoBar.UpdateCloseButtonVisibility(); |
| 193 | + } |
| 194 | + |
| 195 | + private void UpdateIconVisibility() |
| 196 | + { |
| 197 | + Part_IconBorder.Visibility = IsIconVisible ? Visibility.Visible : Visibility.Collapsed; |
| 198 | + } |
| 199 | + |
| 200 | + private void UpdateCloseButtonVisibility() |
| 201 | + { |
| 202 | + PART_CloseButton.Visibility = Closable ? Visibility.Visible : Visibility.Collapsed; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + public enum InfoBarType |
| 207 | + { |
| 208 | + Info, |
| 209 | + Success, |
| 210 | + Warning, |
| 211 | + Error |
| 212 | + } |
| 213 | + |
| 214 | + public enum InfoBarLength |
| 215 | + { |
| 216 | + Short, |
| 217 | + Long |
| 218 | + } |
| 219 | +} |
0 commit comments