@@ -601,92 +601,97 @@ internal string LogoPathFromUri(string uri)
601601 // windows 8.1 https://msdn.microsoft.com/en-us/library/windows/apps/hh965372.aspx#target_size
602602 // windows 8 https://msdn.microsoft.com/en-us/library/windows/apps/br211475.aspx
603603
604- string path ;
605- if ( uri . Contains ( "\\ " ) )
606- {
607- path = Path . Combine ( Package . Location , uri ) ;
608- }
609- else
604+ string path = Path . Combine ( Package . Location , uri ) ;
605+
606+ var logoPath = TryToFindLogo ( uri , path ) ;
607+ if ( String . IsNullOrEmpty ( logoPath ) )
610608 {
609+ // TODO: Don't know why, just keep it at the moment
610+ // Maybe on older version of Windows 10?
611611 // for C:\Windows\MiracastView etc
612- path = Path . Combine ( Package . Location , "Assets" , uri ) ;
612+ return TryToFindLogo ( uri , Path . Combine ( Package . Location , "Assets" , uri ) ) ;
613613 }
614+ return logoPath ;
614615
615- var extension = Path . GetExtension ( path ) ;
616- if ( extension != null )
616+ string TryToFindLogo ( string uri , string path )
617617 {
618- var end = path . Length - extension . Length ;
619- var prefix = path . Substring ( 0 , end ) ;
620- var paths = new List < string >
618+ var extension = Path . GetExtension ( path ) ;
619+ if ( extension != null )
621620 {
622- path
623- } ;
624-
625- var scaleFactors = new Dictionary < PackageVersion , List < int > >
626- {
627- // scale factors on win10: https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets#asset-size-tables,
621+ //if (File.Exists(path))
622+ //{
623+ // return path; // shortcut, avoid enumerating files
624+ //}
625+
626+ var logoNamePrefix = Path . GetFileNameWithoutExtension ( uri ) ; // e.g Square44x44
627+ var logoDir = Path . GetDirectoryName ( path ) ; // e.g ..\..\Assets
628+ if ( String . IsNullOrEmpty ( logoNamePrefix ) || String . IsNullOrEmpty ( logoDir ) || ! Directory . Exists ( logoDir ) )
628629 {
629- PackageVersion . Windows10 , new List < int >
630- {
631- 100 ,
632- 125 ,
633- 150 ,
634- 200 ,
635- 400
636- }
637- } ,
638- {
639- PackageVersion . Windows81 , new List < int >
640- {
641- 100 ,
642- 120 ,
643- 140 ,
644- 160 ,
645- 180
646- }
647- } ,
630+ // Known issue: Edge always triggers it since logo is not at uri
631+ ProgramLogger . LogException ( $ "|UWP|LogoPathFromUri|{ Package . Location } " +
632+ $ "|{ UserModelId } can't find logo uri for { uri } in package location (logo name or directory not found): { Package . Location } ", new FileNotFoundException ( ) ) ;
633+ return string . Empty ;
634+ }
635+
636+ var files = Directory . EnumerateFiles ( logoDir ) ;
637+
638+ // Currently we don't care which one to choose
639+ // Just ignore all qualifiers
640+ // select like logo.[xxx_yyy].png
641+ // https://learn.microsoft.com/en-us/windows/uwp/app-resources/tailor-resources-lang-scale-contrast
642+ var logos = files . Where ( file =>
643+ Path . GetFileName ( file ) ? . StartsWith ( logoNamePrefix , StringComparison . OrdinalIgnoreCase ) ?? false
644+ && extension . Equals ( Path . GetExtension ( file ) , StringComparison . OrdinalIgnoreCase )
645+ ) ;
646+
647+ var selected = logos . FirstOrDefault ( ) ;
648+ var closest = selected ;
649+ int min = int . MaxValue ;
650+ foreach ( var logo in logos )
648651 {
649- PackageVersion . Windows8 , new List < int >
652+
653+ var imageStream = File . OpenRead ( logo ) ;
654+ var decoder = BitmapDecoder . Create ( imageStream , BitmapCreateOptions . IgnoreColorProfile , BitmapCacheOption . None ) ;
655+ var height = decoder . Frames [ 0 ] . PixelHeight ;
656+ var width = decoder . Frames [ 0 ] . PixelWidth ;
657+ int pixelCountDiff = Math . Abs ( height * width - 1936 ) ; // 44*44=1936
658+ if ( pixelCountDiff < min )
650659 {
651- 100
660+ // try to find the closest to 44x44 logo
661+ closest = logo ;
662+ if ( pixelCountDiff == 0 )
663+ break ; // found 44x44
664+ min = pixelCountDiff ;
652665 }
653666 }
654- } ;
655667
656- if ( scaleFactors . ContainsKey ( Package . Version ) )
657- {
658- foreach ( var factor in scaleFactors [ Package . Version ] )
668+ selected = closest ;
669+ if ( ! string . IsNullOrEmpty ( selected ) )
659670 {
660- paths . Add ( $ "{ prefix } .scale-{ factor } { extension } ") ;
671+ return selected ;
672+ }
673+ else
674+ {
675+ ProgramLogger . LogException ( $ "|UWP|LogoPathFromUri|{ Package . Location } " +
676+ $ "|{ UserModelId } can't find logo uri for { uri } in package location (can't find specified logo): { Package . Location } ", new FileNotFoundException ( ) ) ;
677+ return string . Empty ;
661678 }
662- }
663-
664- var selected = paths . FirstOrDefault ( File . Exists ) ;
665- if ( ! string . IsNullOrEmpty ( selected ) )
666- {
667- return selected ;
668679 }
669680 else
670681 {
671682 ProgramLogger . LogException ( $ "|UWP|LogoPathFromUri|{ Package . Location } " +
672- $ "|{ UserModelId } can't find logo uri for { uri } in package location: { Package . Location } ", new FileNotFoundException ( ) ) ;
683+ $ "|Unable to find extension from { uri } for { UserModelId } " +
684+ $ "in package location { Package . Location } ", new FileNotFoundException ( ) ) ;
673685 return string . Empty ;
674686 }
675687 }
676- else
677- {
678- ProgramLogger . LogException ( $ "|UWP|LogoPathFromUri|{ Package . Location } " +
679- $ "|Unable to find extension from { uri } for { UserModelId } " +
680- $ "in package location { Package . Location } ", new FileNotFoundException ( ) ) ;
681- return string . Empty ;
682- }
683688 }
684689
685690
686691 public ImageSource Logo ( )
687692 {
688693 var logo = ImageFromPath ( LogoPath ) ;
689- var plated = PlatedImage ( logo ) ;
694+ var plated = PlatedImage ( logo ) ; // TODO: maybe get plated directly from app package?
690695
691696 // todo magic! temp fix for cross thread object
692697 plated . Freeze ( ) ;
0 commit comments