1+ import Flutter
12import ImageIO
23import SwiftUI
34import UIKit
45
56struct NativeEmojiPickerView : View {
67 let data : NativeEmojiPickerData
78 let appearance : NativeEmojiPickerAppearance
9+ let showGifs : Bool
10+ @ State private var stagingGif = false
11+ @ State private var gifsSelected = false
812 let largeEmoji : Bool
913 let onSelect : ( String ) -> Void
1014 let onSkinToneChanged : ( Int ) -> Void
@@ -26,12 +30,14 @@ struct NativeEmojiPickerView: View {
2630 init (
2731 data : NativeEmojiPickerData ,
2832 appearance : NativeEmojiPickerAppearance ,
33+ showGifs : Bool = false ,
2934 largeEmoji : Bool = false ,
3035 initialSkinTone : Int ,
3136 onSelect : @escaping ( String ) -> Void ,
3237 onSkinToneChanged : @escaping ( Int ) -> Void ,
3338 onClose : @escaping ( ) -> Void
3439 ) {
40+ self . showGifs = showGifs
3541 self . data = data
3642 self . appearance = appearance
3743 self . largeEmoji = largeEmoji
@@ -51,29 +57,67 @@ struct NativeEmojiPickerView: View {
5157 var body : some View {
5258 ScrollViewReader { proxy in
5359 VStack ( spacing : 0 ) {
60+ if showGifs {
61+ ContentSegments ( selection : $gifsSelected )
62+ . disabled ( stagingGif )
63+ . frame ( height : 32 )
64+ . padding ( . horizontal , 16 )
65+ . padding ( . top , 20 )
66+ }
5467 header
55- if query . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty {
68+ if ! showGifs && query . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty {
5669 categoryRail ( proxy )
5770 }
5871 Divider ( ) . overlay ( Color ( uiColor : appearance . divider ) )
59- pickerContent
72+ if gifsSelected {
73+ LiveGIFGrid ( query : query . trimmingCharacters ( in : . whitespacesAndNewlines ) , fallbackColor : appearance . secondaryText ) { slug in
74+ guard gifsSelected , ! stagingGif else { return }
75+ stagingGif = true
76+ Task { @MainActor in
77+ guard stagingGif , gifsSelected else { return }
78+ let staged = await NativeEmojiPickerCoordinator . stageGif ( slug )
79+ if staged && stagingGif && gifsSelected { onClose ( ) }
80+ stagingGif = false
81+ }
82+ }
83+ . disabled ( stagingGif )
84+ } else {
85+ pickerContent . overlay ( alignment : . bottom ) {
86+ if showGifs && query . isEmpty {
87+ if #available( iOS 26 , * ) {
88+ categoryRail ( proxy ) . glassEffect ( . regular , in : Capsule ( ) )
89+ . padding ( . horizontal , 24 )
90+ . padding ( . bottom , 16 )
91+ } else {
92+ categoryRail ( proxy ) . background ( . regularMaterial , in : Capsule ( ) )
93+ . padding ( . horizontal , 24 )
94+ . padding ( . bottom , 16 )
95+ }
96+ }
97+ }
98+ }
6099 }
100+ . ignoresSafeArea ( . container , edges : showGifs ? . bottom : [ ] )
61101 . background ( Color ( uiColor : appearance . surface ) )
62102 }
103+ . interactiveDismissDisabled ( stagingGif )
104+ . onDisappear { stagingGif = false }
63105 }
64106
107+ @ Environment ( \. accessibilityReduceMotion ) private var reduceMotion
108+
65109 private var header : some View {
66110 HStack ( spacing : 8 ) {
67111 HStack ( spacing : 10 ) {
68112 TablerNativeIcon ( symbol : " magnifyingglass " )
69113 . font ( . system ( size : 17 , weight : . medium ) )
70114 . foregroundStyle ( Color ( uiColor : appearance . secondaryText ) )
71- TextField ( " Search emoji " , text : $query )
115+ TextField ( gifsSelected ? " Search GIFs " : " Search emoji " , text : $query )
72116 . textInputAutocapitalization ( . never )
73117 . autocorrectionDisabled ( true )
74118 . submitLabel ( . search )
75119 . foregroundStyle ( Color ( uiColor : appearance . text ) )
76- if ! query . isEmpty {
120+ if ! showGifs && ! query . isEmpty {
77121 Button {
78122 query = " "
79123 } label : {
@@ -92,16 +136,21 @@ struct NativeEmojiPickerView: View {
92136 . stroke ( Color ( uiColor : appearance . divider ) , lineWidth : 1 )
93137 }
94138
95- Button ( action : onClose ) {
139+ if ! showGifs || ! query . isEmpty {
140+ Button ( action : { if showGifs { query = " " } else { onClose ( ) } } ) {
96141 TablerNativeIcon ( symbol : " xmark " )
97142 . font ( . system ( size : 17 , weight : . semibold ) )
98143 . foregroundStyle ( Color ( uiColor : appearance . text ) )
99144 . frame ( width : 44 , height : 44 )
100145 . background ( Color ( uiColor : appearance . control ) , in : Circle ( ) )
146+ . overlay { Circle ( ) . strokeBorder ( Color ( uiColor : appearance . divider ) , lineWidth : 1 ) }
101147 }
102148 . buttonStyle ( . plain )
103- . accessibilityLabel ( " Close sheet " )
149+ . accessibilityLabel ( showGifs ? " Clear search " : " Close sheet " )
150+ . transition ( . scale ( scale : 0.75 ) . combined ( with : . opacity ) )
151+ }
104152 }
153+ . animation ( showGifs && ! reduceMotion ? . easeOut ( duration : 0.16 ) : nil , value : query . isEmpty )
105154 . padding ( . horizontal , 16 )
106155 . padding ( . top , 16 )
107156 . padding ( . bottom , 8 )
@@ -113,6 +162,9 @@ struct NativeEmojiPickerView: View {
113162 NativeEmojiCategoryButton (
114163 section : section ,
115164 appearance : appearance ,
165+ highlightColor : showGifs ? appearance . surface : appearance . control ,
166+ outlineColor : showGifs ? appearance . neutral4 : nil ,
167+ selectedIconColor : showGifs ? appearance . text : appearance . accent ,
116168 selection : categorySelection
117169 ) {
118170 categorySelection . select ( section . id )
@@ -128,8 +180,9 @@ struct NativeEmojiPickerView: View {
128180 skinToneSelector
129181 . frame ( maxWidth : . infinity )
130182 }
131- . padding ( . horizontal , 16 )
132- . frame ( height : 44 )
183+ . frame ( height : 36 )
184+ . padding ( . vertical , 4 )
185+ . padding ( . horizontal , showGifs ? 4 : 16 )
133186 }
134187
135188 private var skinToneSelector : some View {
@@ -284,7 +337,7 @@ struct NativeEmojiPickerView: View {
284337 }
285338 }
286339 }
287- . padding ( . bottom , 8 )
340+ . padding ( . bottom , showGifs && query . isEmpty ? 68 : 8 )
288341 . background ( contentBoundaryReporter ( ) )
289342 }
290343 . coordinateSpace ( name : sectionListSpace )
@@ -379,6 +432,9 @@ struct NativeEmojiPickerView: View {
379432private struct NativeEmojiCategoryButton : View {
380433 let section : NativeEmojiSection
381434 let appearance : NativeEmojiPickerAppearance
435+ let highlightColor : UIColor
436+ let outlineColor : UIColor ?
437+ let selectedIconColor : UIColor
382438 @ ObservedObject var selection : NativeEmojiCategorySelection
383439 let onSelect : ( ) -> Void
384440
@@ -390,15 +446,20 @@ private struct NativeEmojiCategoryButton: View {
390446 . foregroundStyle (
391447 Color (
392448 uiColor : isSelected
393- ? appearance . accent : appearance . secondaryText
449+ ? selectedIconColor : appearance . secondaryText
394450 )
395451 )
396452 . frame ( maxWidth : . infinity )
397453 . frame ( height : 36 )
398454 . background (
399- isSelected ? Color ( uiColor : appearance . control ) : Color . clear ,
455+ isSelected ? Color ( uiColor : highlightColor ) : Color . clear ,
400456 in : Circle ( )
401457 )
458+ . overlay {
459+ if isSelected , let outlineColor {
460+ Circle ( ) . strokeBorder ( Color ( uiColor : outlineColor ) , lineWidth : 1 )
461+ }
462+ }
402463 }
403464 . buttonStyle ( . plain )
404465 . accessibilityLabel ( section . title )
@@ -409,6 +470,7 @@ private struct NativeEmojiCategoryButton: View {
409470struct NativeEmojiRemoteImage : View {
410471 let url : URL
411472 let fallbackColor : UIColor
473+ var maximumPixelSize = 84
412474
413475 @ State private var phase : Phase = . loading
414476
@@ -432,7 +494,7 @@ struct NativeEmojiRemoteImage: View {
432494 }
433495 . task ( id : url ) {
434496 do {
435- phase = . success ( try await NativeEmojiRemoteImageLoader . image ( for : url ) )
497+ phase = . success ( try await NativeEmojiRemoteImageLoader . image ( for : url , maximumPixelSize : maximumPixelSize ) )
436498 } catch {
437499 if ! Task . isCancelled { phase = . failure }
438500 }
@@ -475,7 +537,7 @@ enum NativeEmojiRemoteImageLoader {
475537 private static let maximumPixelSize = 84
476538
477539 @ MainActor
478- static func image ( for url : URL ) async throws -> UIImage {
540+ static func image ( for url : URL , maximumPixelSize : Int = 84 ) async throws -> UIImage {
479541 let bytes = try await NativeEmojiPickerCoordinator . mediaBytes ( for : url )
480542 // Bytes can arrive after the cell left; don't decode for a dead view
481543 // while visible cells wait for the decode threads.
@@ -548,3 +610,84 @@ private struct TablerNativeIcon: View {
548610 }
549611 }
550612}
613+
614+ private struct ContentSegments : UIViewRepresentable {
615+ @ Binding var selection : Bool
616+
617+ func makeUIView ( context : Context ) -> UISegmentedControl {
618+ let control = UISegmentedControl ( items : [ " Emoji " , " GIFs " ] )
619+ control . addAction ( UIAction { [ weak control ] _ in
620+ selection = control ? . selectedSegmentIndex == 1
621+ } , for : . valueChanged )
622+ return control
623+ }
624+
625+ func updateUIView ( _ control : UISegmentedControl , context : Context ) {
626+ let index = selection ? 1 : 0
627+ if control . selectedSegmentIndex != index { control . selectedSegmentIndex = index }
628+ }
629+ }
630+
631+ private struct LiveGIFGrid : View {
632+ let query : String
633+ let fallbackColor : UIColor
634+ let onSelect : ( String ) -> Void
635+ @ State private var rows : [ [ String : String ] ] = [ ]
636+ @ State private var loading = true
637+ @ State private var failed = false
638+
639+
640+ var body : some View {
641+ VStack {
642+ ScrollView {
643+ if loading {
644+ ProgressView ( ) . padding ( )
645+ } else if failed {
646+ Text ( " GIF search unavailable " ) . padding ( )
647+ } else if rows . isEmpty {
648+ Text ( " No GIFs found " ) . padding ( )
649+ } else {
650+ LazyVGrid ( columns : [ GridItem ( . flexible ( ) ) , GridItem ( . flexible ( ) ) ] ) {
651+ ForEach ( rows . indices , id : \. self ) { index in
652+ if let raw = rows [ index ] [ " url " ] , let url = URL ( string : raw ) {
653+ Button {
654+ if let slug = rows [ index ] [ " slug " ] { onSelect ( slug ) }
655+ } label : {
656+ NativeEmojiRemoteImage ( url : url , fallbackColor : fallbackColor , maximumPixelSize : 300 )
657+ . id ( url )
658+ . allowsHitTesting ( false )
659+ . frame ( maxWidth : . infinity )
660+ . frame ( height : 136 )
661+ . clipShape ( RoundedRectangle ( cornerRadius : 10 ) )
662+ . contentShape ( Rectangle ( ) )
663+ }
664+ . buttonStyle ( . plain )
665+ . accessibilityLabel ( rows [ index ] [ " title " ] ?? " GIF " )
666+ . accessibilityIdentifier ( " gif-result " )
667+ }
668+ }
669+ }
670+ . padding ( 16 )
671+ }
672+ }
673+ Text ( " Powered by KLIPY " )
674+ . font ( . footnote )
675+ . foregroundStyle ( . secondary )
676+ . padding ( . bottom , 16 )
677+ }
678+ . task ( id : query ) {
679+ loading = true
680+ do {
681+ if ! query . isEmpty { try await Task . sleep ( nanoseconds : 400_000_000 ) }
682+ let result = try await NativeEmojiPickerCoordinator . searchGifs ( query )
683+ try Task . checkCancellation ( )
684+ rows = result
685+ failed = false
686+ } catch {
687+ guard ! Task . isCancelled else { return }
688+ failed = true
689+ }
690+ loading = false
691+ }
692+ }
693+ }
0 commit comments