forked from microsoft/Windows-appsample-photo-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.cpp
245 lines (211 loc) · 9.53 KB
/
MainPage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
// ---------------------------------------------------------------------------------
#include "pch.h"
#include "MainPage.h"
#include "Photo.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Search;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Composition;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Hosting;
using namespace Windows::UI::Xaml::Media::Animation;
using namespace Windows::UI::Xaml::Media::Imaging;
namespace winrt::PhotoEditor::implementation
{
// Page constructor.
MainPage::MainPage() :
m_photos(winrt::single_threaded_observable_vector<IInspectable>()),
m_compositor(Window::Current().Compositor())
{
InitializeComponent();
ParaView().Source(ForegroundElement());
}
// Loads collection of Photos from users Pictures library.
IAsyncAction MainPage::OnNavigatedTo(NavigationEventArgs e)
{
// Load photos if they haven't previously been loaded.
if (Photos().Size() == 0)
{
m_elementImplicitAnimation = m_compositor.CreateImplicitAnimationCollection();
// Define trigger and animation that should play when the trigger is triggered.
m_elementImplicitAnimation.Insert(L"Offset", CreateOffsetAnimation());
co_await GetItemsAsync();
}
}
IAsyncAction MainPage::OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
auto elementVisual = ElementCompositionPreview::GetElementVisual(args.ItemContainer());
auto image = args.ItemContainer().ContentTemplateRoot().as<Image>();
if (args.InRecycleQueue())
{
elementVisual.ImplicitAnimations(nullptr);
image.Source(nullptr);
}
if (args.Phase() == 0)
{
//Add implicit animation to each visual.
elementVisual.ImplicitAnimations(m_elementImplicitAnimation);
args.RegisterUpdateCallback([&](auto sender, auto args)
{
OnContainerContentChanging(sender, args);
});
args.Handled(true);
}
if (args.Phase() == 1)
{
// It's phase 1, so show this item's image.
image.Opacity(100);
auto item = unbox_value<PhotoEditor::Photo>(args.Item());
Photo* impleType = from_abi<Photo>(item);
try
{
auto thumbnail = co_await impleType->GetImageThumbnailAsync();
image.Source(thumbnail);
}
catch (winrt::hresult_error)
{
// File could be corrupt, or it might have an image file
// extension, but not really be an image file.
BitmapImage bitmapImage{};
Uri uri{ image.BaseUri().AbsoluteUri(), L"Assets/StoreLogo.png" };
bitmapImage.UriSource(uri);
image.Source(bitmapImage);
}
}
}
// Called by the Loaded event of the ImageGridView for animation after back navigation from DetailPage view.
IAsyncAction MainPage::StartConnectedAnimationForBackNavigation()
{
// Run the connected animation for navigation back to the main page from the detail page.
if (m_persistedItem)
{
ImageGridView().ScrollIntoView(m_persistedItem);
auto animation = ConnectedAnimationService::GetForCurrentView().GetAnimation(L"backAnimation");
if (animation)
{
co_await ImageGridView().TryStartConnectedAnimationAsync(animation, m_persistedItem, L"ItemImage");
}
}
}
// Registers property changed event handler.
event_token MainPage::PropertyChanged(PropertyChangedEventHandler const& value)
{
return m_propertyChanged.add(value);
}
// Unregisters property changed event handler.
void MainPage::PropertyChanged(event_token const& token)
{
m_propertyChanged.remove(token);
}
// Loads images from the user's Pictures library.
IAsyncAction MainPage::GetItemsAsync()
{
// Show the loading progress bar.
LoadProgressIndicator().Visibility(Windows::UI::Xaml::Visibility::Visible);
NoPicsText().Visibility(Windows::UI::Xaml::Visibility::Collapsed);
// File type filter.
QueryOptions options{};
options.FolderDepth(FolderDepth::Deep);
options.FileTypeFilter().Append(L".jpg");
options.FileTypeFilter().Append(L".png");
options.FileTypeFilter().Append(L".gif");
// Get the Pictures library.
StorageFolder picturesFolder = KnownFolders::PicturesLibrary();
auto result = picturesFolder.CreateFileQueryWithOptions(options);
auto imageFiles = co_await result.GetFilesAsync();
auto unsupportedFilesFound = false;
// Populate Photos collection.
for (auto&& file : imageFiles)
{
// Only files on the local computer are supported.
// Files on OneDrive or a network location are excluded.
if (file.Provider().Id() == L"computer")
{
auto image = co_await LoadImageInfoAsync(file);
Photos().Append(image);
}
else
{
unsupportedFilesFound = true;
}
}
if (Photos().Size() == 0)
{
// No pictures were found in the library, so show message.
NoPicsText().Visibility(Windows::UI::Xaml::Visibility::Visible);
}
// Hide the loading progress bar.
LoadProgressIndicator().Visibility(Windows::UI::Xaml::Visibility::Collapsed);
if (unsupportedFilesFound)
{
ContentDialog unsupportedFilesDialog{};
unsupportedFilesDialog.Title(box_value(L"Unsupported images found"));
unsupportedFilesDialog.Content(box_value(L"This sample app only supports images stored locally on the computer. We found files in your library that are stored in OneDrive or another network location. We didn't load those images."));
unsupportedFilesDialog.CloseButtonText(L"Ok");
co_await unsupportedFilesDialog.ShowAsync();
}
}
// Creates a Photo from Storage file for adding to Photo collection.
IAsyncOperation<PhotoEditor::Photo> MainPage::LoadImageInfoAsync(StorageFile file)
{
auto properties = co_await file.Properties().GetImagePropertiesAsync();
auto info = winrt::make<Photo>(properties, file, file.DisplayName(), file.DisplayType());
co_return info;
}
CompositionAnimationGroup MainPage::CreateOffsetAnimation()
{
//Define Offset Animation for the Animation group.
Vector3KeyFrameAnimation offsetAnimation = m_compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.InsertExpressionKeyFrame(1.0f, L"this.FinalValue");
TimeSpan span{ std::chrono::milliseconds{400} };
offsetAnimation.Duration(span);
//Define Animation Target for this animation to animate using definition.
offsetAnimation.Target(L"Offset");
//Add Animations to Animation group.
CompositionAnimationGroup animationGroup = m_compositor.CreateAnimationGroup();
animationGroup.Add(offsetAnimation);
return animationGroup;
}
// Photo clicked event handler for navigation to DetailPage view.
void MainPage::ImageGridView_ItemClick(IInspectable const sender, ItemClickEventArgs const e)
{
// Prepare the connected animation for navigation to the detail page.
m_persistedItem = e.ClickedItem().as<PhotoEditor::Photo>();
ImageGridView().PrepareConnectedAnimation(L"itemAnimation", e.ClickedItem(), L"ItemImage");
auto m_suppress = SuppressNavigationTransitionInfo();
Frame().Navigate(xaml_typename<PhotoEditor::DetailPage>(), e.ClickedItem(), m_suppress);
}
// Triggers property changed notification.
void MainPage::RaisePropertyChanged(hstring const& propertyName)
{
m_propertyChanged(*this, PropertyChangedEventArgs(propertyName));
}
}