OSDN Git Service

あるバグの再現のためにドキュメントを追加できるようにした
[fooeditengine/FooEditEngine.git] / UWP / Test / MainPage.xaml.cs
1 using System;
2 using System.IO;
3 using System.Threading.Tasks;
4 using System.Runtime.InteropServices.WindowsRuntime;
5 using Windows.Foundation;
6 using Windows.UI.Core;
7 using Windows.UI.Xaml;
8 using Windows.UI.Xaml.Controls;
9 using Windows.UI.ViewManagement;
10 using Windows.Storage;
11 using Windows.Storage.Pickers;
12 using Windows.Graphics.Printing;
13 using Windows.Graphics.Printing.OptionDetails;
14 using FooEditEngine.UWP;
15
16 // 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください
17
18 namespace Test
19 {
20     /// <summary>
21     /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
22     /// </summary>
23     public sealed partial class MainPage : Page
24     {
25         MainViewModel vm = new MainViewModel();
26         public MainPage()
27         {
28             this.InitializeComponent();
29             this.DataContext = this.vm;
30             //this.fooTextBox.AllowFocusOnInteraction = true;
31             PrintManager.GetForCurrentView().PrintTaskRequested += MainPage_PrintTaskRequested;
32             InputPane currentView = InputPane.GetForCurrentView();
33             currentView.Showing += currentView_Showing;
34             currentView.Hiding += currentView_Hiding;
35         }
36
37         void currentView_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
38         {
39             this.Margin = new Thickness(0);
40             args.EnsuredFocusedElementInView = true;
41         }
42
43         void currentView_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
44         {
45             this.Margin = new Thickness(0, 0, 0, args.OccludedRect.Height);
46             args.EnsuredFocusedElementInView = true;
47         }
48
49         void MainPage_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
50         {
51             IAsyncAction async = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
52             {
53                 var doc = this.vm.CurrentDocument;
54                 var source = new DocumentSource(doc, new FooEditEngine.Padding(20, 20, 20, 20), this.FontFamily.Source, this.FontSize);
55                 source.ParseHF = (s,e)=> { return e.Original; };
56                 source.Header = "header";
57                 source.Fotter = "footer";
58                 source.Forground = new Windows.UI.Color() { R = 0, A = 255, B = 0, G = 0};
59                 source.Keyword1 = new Windows.UI.Color() { R = 0, A = 255, B = 255, G = 0 };
60                 source.Keyword2 = new Windows.UI.Color() { R = 128, A = 255, B = 255, G = 0 };
61                 source.Literal = new Windows.UI.Color() { R = 128, A = 255, B = 128, G = 0 };
62                 source.Comment = new Windows.UI.Color() { R = 255, A = 255, B = 0, G = 0 };
63                 source.Url = new Windows.UI.Color() { R = 0, A = 255, B = 255, G = 0 };
64
65                 PrintTask task = null;
66                 task = args.Request.CreatePrintTask("test", (e) =>
67                 {
68                     e.SetSource(source);
69                 });
70                 task.Completed += async (s, e) => {
71                     source.Dispose();
72                     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
73                         System.Diagnostics.Debug.WriteLine("finished printing");
74                     });
75                 };
76                 PrintOptionBuilder<DocumentSource> builder = new PrintOptionBuilder<DocumentSource>(source);
77                 builder.BuildPrintOption(PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options));
78             });
79             Task t = WindowsRuntimeSystemExtensions.AsTask(async);
80             t.Wait();
81         }
82
83         private async void Button_Click(object sender, RoutedEventArgs e)
84         {
85             var filepicker = new FileOpenPicker();
86             filepicker.FileTypeFilter.Add(".txt");
87             var file = await filepicker.PickSingleFileAsync();
88             if(file != null)
89             {
90                 using (var ws = await file.OpenAsync(FileAccessMode.Read))
91                 using (var fs = new StreamReader(ws.AsStream()))
92                 {
93                     await this.vm.CurrentDocument.LoadAsync(fs, null);
94                 }
95                 this.vm.CurrentDocument.RequestRedraw();
96             }
97         }
98
99         private async void Print_Button_Click(object sender, RoutedEventArgs e)
100         {
101             await PrintManager.ShowPrintUIAsync();
102         }
103
104         private void Button_Click_New(object sender, RoutedEventArgs e)
105         {
106             this.vm.AddDocument();
107         }
108     }
109 }