|
| 1 | +// ****************************************************************** |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// This code is licensed under the MIT License (MIT). |
| 4 | +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 5 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 7 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 8 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 9 | +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 10 | +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. |
| 11 | +// ****************************************************************** |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Threading.Tasks; |
| 15 | +using Windows.ApplicationModel.Core; |
| 16 | +using Windows.Foundation.Metadata; |
| 17 | +using Windows.System; |
| 18 | +using Windows.UI.Shell; |
| 19 | +using Windows.UI.StartScreen; |
| 20 | + |
| 21 | +namespace Microsoft.Toolkit.Uwp.Helpers |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Enumeration listing all Pin results |
| 25 | + /// </summary> |
| 26 | + public enum PinResult |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Unsupported Device |
| 30 | + /// </summary> |
| 31 | + UnsupportedDevice = 0, |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Unsupported Windows 10 OS ( Pin support Version StartMenu >= 15063 ,TaskBar >= 16299) |
| 35 | + /// </summary> |
| 36 | + UnsupportedOs = 1, |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// pin access is denied |
| 40 | + /// </summary> |
| 41 | + PinNotAllowed = 2, |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// App has added startMenu or TaskBar |
| 45 | + /// </summary> |
| 46 | + PinPresent = 3, |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// App has already is avaliable in StartMenu orTaskBar |
| 50 | + /// </summary> |
| 51 | + PinAlreadyPresent = 4 |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// This class provides static helper methods help to add the app in startmenu or TaskBar. |
| 56 | + /// </summary> |
| 57 | + public static class AppPinManager |
| 58 | + { |
| 59 | + /// <summary> |
| 60 | + /// Pin the current app in Windows TaskBar |
| 61 | + /// </summary> |
| 62 | + /// <returns>PinResult</returns> |
| 63 | + public static async Task<PinResult> CurrentAppToTaskBarAsync() |
| 64 | + { |
| 65 | + var pinResult = PinResult.UnsupportedOs; |
| 66 | + |
| 67 | + if (ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager")) |
| 68 | + { |
| 69 | + if (TaskbarManager.GetDefault().IsSupported) |
| 70 | + { |
| 71 | + if (TaskbarManager.GetDefault().IsPinningAllowed) |
| 72 | + { |
| 73 | + if (await TaskbarManager.GetDefault().IsCurrentAppPinnedAsync()) |
| 74 | + { |
| 75 | + pinResult = PinResult.PinAlreadyPresent; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + if (await TaskbarManager.GetDefault().RequestPinCurrentAppAsync()) |
| 80 | + { |
| 81 | + pinResult = PinResult.PinPresent; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + pinResult = PinResult.PinAlreadyPresent; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + pinResult = PinResult.PinNotAllowed; |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + pinResult = PinResult.UnsupportedDevice; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return pinResult; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Pin Specific App in Windows TaskBar |
| 105 | + /// </summary> |
| 106 | + /// <param name="appListEntry">AppListEntry</param> |
| 107 | + /// <returns>PinResult</returns> |
| 108 | + public static async Task<PinResult> SpecificAppToTaskBarAsync(AppListEntry appListEntry) |
| 109 | + { |
| 110 | + var pinResult = PinResult.UnsupportedOs; |
| 111 | + |
| 112 | + if (ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager")) |
| 113 | + { |
| 114 | + if (TaskbarManager.GetDefault().IsSupported) |
| 115 | + { |
| 116 | + if (TaskbarManager.GetDefault().IsPinningAllowed) |
| 117 | + { |
| 118 | + if (await TaskbarManager.GetDefault().IsAppListEntryPinnedAsync(appListEntry)) |
| 119 | + { |
| 120 | + pinResult = PinResult.PinAlreadyPresent; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + if (await TaskbarManager.GetDefault().RequestPinAppListEntryAsync(appListEntry)) |
| 125 | + { |
| 126 | + pinResult = PinResult.PinPresent; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + pinResult = PinResult.PinAlreadyPresent; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + pinResult = PinResult.PinNotAllowed; |
| 137 | + } |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + pinResult = PinResult.UnsupportedDevice; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return pinResult; |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Pin Specific App in Windows StartMenu |
| 150 | + /// </summary> |
| 151 | + /// <param name="entry">AppListEntry</param> |
| 152 | + /// <returns>PinResult</returns> |
| 153 | + public static async Task<PinResult> SpecificAppToStartMenuAsync(AppListEntry entry) |
| 154 | + { |
| 155 | + var resultPinResult = PinResult.UnsupportedOs; |
| 156 | + if (ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager")) |
| 157 | + { |
| 158 | + if (StartScreenManager.GetDefault().SupportsAppListEntry(entry)) |
| 159 | + { |
| 160 | + if (await StartScreenManager.GetDefault().ContainsAppListEntryAsync(entry)) |
| 161 | + { |
| 162 | + resultPinResult = PinResult.PinAlreadyPresent; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + if (await StartScreenManager.GetDefault().RequestAddAppListEntryAsync(entry)) |
| 167 | + { |
| 168 | + resultPinResult = PinResult.PinPresent; |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + resultPinResult = PinResult.PinAlreadyPresent; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + resultPinResult = PinResult.UnsupportedDevice; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return resultPinResult; |
| 183 | + } |
| 184 | + |
| 185 | + /// <summary> |
| 186 | + /// Pin Specific App in Windows StartMenu based on User |
| 187 | + /// </summary> |
| 188 | + /// <param name="user">User</param> |
| 189 | + /// <param name="entry">AppListEntry</param> |
| 190 | + /// <returns>PinResult</returns> |
| 191 | + public static async Task<PinResult> UserSpecificAppToStartMenuAsync(User user, AppListEntry entry) |
| 192 | + { |
| 193 | + var resultPinResult = PinResult.UnsupportedOs; |
| 194 | + if (ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager")) |
| 195 | + { |
| 196 | + if (StartScreenManager.GetForUser(user).SupportsAppListEntry(entry)) |
| 197 | + { |
| 198 | + if (await StartScreenManager.GetForUser(user).ContainsAppListEntryAsync(entry)) |
| 199 | + { |
| 200 | + resultPinResult = PinResult.PinAlreadyPresent; |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + if (await StartScreenManager.GetForUser(user).RequestAddAppListEntryAsync(entry)) |
| 205 | + { |
| 206 | + resultPinResult = PinResult.PinPresent; |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + resultPinResult = PinResult.PinAlreadyPresent; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + resultPinResult = PinResult.UnsupportedDevice; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return resultPinResult; |
| 221 | + } |
| 222 | + |
| 223 | + } |
| 224 | +} |
0 commit comments