forked from dart-archive/dart-up-and-running-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch04.xml
1235 lines (985 loc) · 46.9 KB
/
ch04.xml
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<chapter id="ch04">
<title>Tools</title>
<para>Dart provides several tools to help you write and deploy your web and
command-line apps.</para>
<variablelist>
<varlistentry>
<term><link linkend="ch04-tools-pub">pub: The Dart package
manager</link></term>
<listitem>
<para>Download and install packages of libraries.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><link linkend="ch04-tools-editor">Dart Editor</link></term>
<listitem>
<para>Edit, run, and debug web and command-line apps.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><link linkend="ch04-tools-dartium">Dartium: Chromium with the Dart
VM </link></term>
<listitem>
<para>Run Dart web apps. This is a special build of Chromium (the
project behind Google Chrome).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><link linkend="ch04-tools-dart2js">dart2js: The Dart-to-JavaScript
compiler</link></term>
<listitem>
<para>Convert your web app to JavaScript, so it can run in non-Dartium
browsers.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><link linkend="ch04-tools-dart-vm">dart: The standalone Dart
VM</link></term>
<listitem>
<para>Run your command-line apps—server-side scripts, programs,
servers, and any other apps that don’t run in a browser.</para>
</listitem>
</varlistentry>
</variablelist>
<para>All of these tools are in the Dart Editor bundle, since the editor
uses Dartium and the other tools. You can also download Dartium separately,
and you can download an SDK that includes dart2js, dart, and pub. See the
<ulink url="http://www.dartlang.org/downloads.html">Downloads page</ulink>
for links and details.</para>
<sect1 id="ch04-tools-pub">
<title>pub: The Dart Package Manager</title>
<para>You can use use the <emphasis>pub</emphasis> tool (<literal
moreinfo="none"><replaceable>$DART_SDK</replaceable>/bin/pub</literal>) to
manage Dart packages. A Dart package is simply a directory containing any
number of Dart libraries and a list of the library dependencies. A package
can also contain resources for its libraries, such as documentation,
tests, and images. If your app uses one or more packages, then your app
itself must be a package.</para>
<para>A package can live anywhere. Some packages are distributed in the
Dart SDK, under the <literal moreinfo="none">pkg</literal> directory.
Others are on GitHub. We plan to publish packages at<phrase
role="keep-together"> <ulink
url="http://pub.dartlang.org">pub.dartlang.org</ulink>,</phrase> and we
hope you will, too.</para>
<para>To use a library that’s in a Dart package, you need to do the
following:</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>Create a pubspec (a file that lists package
dependencies).</para>
</listitem>
<listitem>
<para>Use pub to install the package.</para>
</listitem>
<listitem>
<para>Import the library.</para>
</listitem>
</orderedlist>
<sect2 id="ch04-tools-pub-pubspec">
<title>Creating a Pubspec</title>
<para>To use a package, your application must define a pubspec that
lists dependencies and their download locations. The pubspec is a file
named <literal moreinfo="none">pubspec.yaml</literal>, and it must be in
the top directory of your application.</para>
<para>Here is an example of a pubspec that specifies the locations of
two packages. First, it points to the js package that’s hosted on
pub.dartlang.org, and then it points to the intl package in the Dart
SDK.</para>
<programlisting format="linespecific"><remark>lang-js
</remark>name: my_app
dependencies:
js:
hosted: js
intl:
sdk: intl</programlisting>
<para>For details, see the <ulink
url="http://pub.dartlang.org/doc/pubspec.html">pubspec
documentation</ulink> and the documentation for the packages you’re
interested in using.</para>
</sect2>
<sect2 id="ch04-tools-pub-install">
<title>Installing Packages</title>
<para>Once you have a pubspec, you can run <literal moreinfo="none">pub
install</literal> from the top directory of your application.</para>
<screen format="linespecific"><remark>lang-sh
</remark>cd <replaceable>my/app</replaceable>
<replaceable>$DART_SDK</replaceable>/bin/pub install</screen>
<para>This command determines which packages your app depends on, and
puts them in a central cache. For git dependencies, pub clones the git
repository. For hosted dependencies, pub downloads the package from
pub.dartlang.org. Transitive dependencies are included, too. For
example, if the js package is dependent on the unittest package, the pub
tool grabs both the js package and the unittest package.</para>
<para>Finally, pub creates a <literal moreinfo="none">packages</literal>
directory (under your app’s top directory) that has links to the
packages that your app depends on.</para>
</sect2>
<sect2 id="ch04-tools-pub-import">
<title>Importing Libraries from Packages</title>
<para>To import libraries found in packages, use the <literal
moreinfo="none">package:</literal> prefix.</para>
<programlisting format="linespecific"><remark>lang-dart
</remark>import 'package:js/js.dart' as js;
import 'package:intl/intl.dart';</programlisting>
<para>The Dart runtime takes everything after <literal
moreinfo="none">package:</literal> and looks it up within the <literal
moreinfo="none">packages</literal> directory for your app.</para>
</sect2>
<sect2 id="ch04-tools-pub-more-options">
<title>More Information</title>
<para>Run <literal
moreinfo="none"><replaceable>$DART_SDK</replaceable>/bin/pub
--help</literal> for a list of commands. For more information about pub,
see the <ulink url="http://pub.dartlang.org/doc">pub
documentation.</ulink></para>
</sect2>
</sect1>
<sect1 id="ch04-tools-editor">
<title>Dart Editor</title>
<para>We already introduced Dart Editor in <xref linkend="ch01-editor" />.
Here are some more tips on using Dart Editor, with information such as
<link linkend="ch04-editor-run-non-dartium">specifying a browser</link>
and <link linkend="ch04-editor-dart2js">compiling to JavaScript</link>. If
you run into a problem, see <ulink
url="http://www.dartlang.org/docs/editor/troubleshoot.html">Troubleshooting
Dart Editor.</ulink> Dart Editor is updated frequently, so it probably
looks different from what you see here. For the latest information, see
the <ulink url="http://www.dartlang.org/editor">Dart Editor
documentation.</ulink></para>
<sect2 id="ch04-editor-view-samples">
<title>Viewing Samples</title>
<para>The Welcome page of Dart Editor (<xref
linkend="DartEditor-initial" />) displays a few samples. To open a
sample and look at its source code, click the sample’s image.</para>
<para>If you don’t see the Welcome page, you probably closed it. Get it
back with <emphasis role="bold">Tools > Welcome
Page</emphasis>.</para>
</sect2>
<sect2 id="ch04-editor-apps">
<title>Managing the Files View</title>
<para>The Files view shows the files that implement the libraries
included in Dart, as well as all the apps that you create or
open.</para>
<sect3 id="ch04-editor-apps-open">
<title>Adding apps</title>
<para>Here’s how to open an app, which makes it appear in your Files
view:</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>Go to the <emphasis role="bold">File</emphasis> menu, and
choose <emphasis role="bold">Open Folder...</emphasis>. Or use the
keyboard shortcut (<phrase role="keep-together"><emphasis
role="bold">Ctrl+O</emphasis></phrase> or, on Mac, <emphasis
role="bold">Cmd+O</emphasis>).</para>
</listitem>
<listitem>
<para>Select the directory that contains the app’s files, and
click <emphasis role="bold">Open</emphasis>.</para>
</listitem>
</orderedlist>
<para>The directory and all its files appear in the Files view.</para>
</sect3>
<sect3 id="ch04-editor-apps-remove">
<title>Removing apps</title>
<para>You can remove an app from the Files view, either with or
without deleting its files.</para>
<para>Right-click (or Ctrl+click) the directory and choose <emphasis
role="bold">Delete</emphasis>. If you want to delete the app’s files
permanently, then in the dialog that comes up, choose <emphasis
role="bold">Delete projects on disk</emphasis>.</para>
</sect3>
</sect2>
<sect2 id="ch04-editor-create-app">
<title>Creating Apps</title>
<para>It’s easy to create a simple web or command-line app from
scratch:</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>Click the New Application button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/newapp.png" width="0.12in" />
</imageobject>
</inlinemediaobject> (at the upper left of Dart Editor).
Alternatively, choose <emphasis role="bold">File > New
Application...</emphasis> from the Dart Editor menu. A dialog
appears.</para>
</listitem>
<listitem>
<para>Type in a name for your application—for example,
<code>HelloWeb</code>. If you don’t like the default directory, type
in a new location or browse to choose the location.</para>
</listitem>
<listitem>
<para>If you’re creating a web app, select <emphasis
role="bold">Generate content for a basic web app</emphasis>. If you
want to use the pub package manager, select <emphasis
role="bold">Add pub support</emphasis>. Then click <emphasis
role="bold">Finish</emphasis> to create a directory with initial
files for the app.</para>
<para>A default Dart file appears in the Edit view, and its
directory appears in the Files view. Your Dart Editor window should
look something like <xref linkend="DartEditor-helloweb" />.</para>
<figure float="0" id="DartEditor-helloweb">
<title>A new app, pre-filled with basic, runnable code</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0401.png" format="PNG"
scale="75" />
</imageobject>
</mediaobject>
</figure>
</listitem>
</orderedlist>
</sect2>
<sect2 id="ch04-editor-edit">
<title>Editing Apps</title>
<para>Dart Editor provides the basic editing functionality you’d expect,
plus features such as Dart code completion, API browsing, support for
refactoring, and the ability to search multiple files.</para>
<sect3 id="ch04-editor-edit-autocomplete">
<title>Using autocomplete</title>
<para>Autocomplete suggestions look something like <xref
linkend="helloweb-dotcomplete" />. They appear when you either:</para>
<itemizedlist>
<listitem>
<para>Type a class or variable name, and then type a
period.</para>
<para>For example, type <code>document.</code> or
<code>Date.</code> and pause a moment. Once the suggestions
appear, continue typing to pare down the list.</para>
</listitem>
<listitem>
<para>Type <emphasis role="bold">Ctrl+Space</emphasis>.</para>
<para>For example, type <code>Dat</code>, then Ctrl+Space to see a
list of classes that start with <quote>Dat</quote>.</para>
</listitem>
</itemizedlist>
<para>When the suggestions come up, you can click, type, or use the
arrow keys to select the one you want. Press Enter or Return to choose
a suggestion, or Esc to dismiss the panel.</para>
<figure float="none" id="helloweb-dotcomplete">
<title>Autocomplete suggestions</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0402.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</sect3>
<sect3 id="ch04-editor-edit-browse-apis">
<title>Browsing APIs</title>
<para>With Dart Editor you can easily find where APIs are declared.
You can also outline the contents of a Dart file.</para>
<sect4 id="browse-api-declaration">
<title>Finding where an API is declared</title>
<para>Use this feature to go to the declaration of an API
item—variable, method, type, library, and so on—either within the
same .dart file or in another file.</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>In the Edit view of a Dart file, hold down the <emphasis
role="bold">Control</emphasis> key (<emphasis
role="bold">Command</emphasis> key on Mac) and move the mouse
over the source code. As <xref linkend="sunflower-hover" />
shows, any API item under your cursor is underlined.</para>
<figure float="0" id="sunflower-hover">
<title>Control or Command while hovering over API
items</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0403.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</listitem>
<listitem>
<para>Continuing to press Control or Command, hover over an
underlined string, and click.</para>
<para>The editor displays the file that declares the item. For
example, if you Command-click cos, the file that declares the
<literal>cos()</literal> function appears.</para>
</listitem>
</orderedlist>
</sect4>
<sect4 id="browse-api-outline">
<title>Outlining a file’s contents</title>
<para>Either press <emphasis role="bold">Alt+O</emphasis> (<emphasis
role="bold">Option+O</emphasis> on Mac) or right-click and choose
<emphasis role="bold">Quick Outline</emphasis>.</para>
<para>A panel comes up displaying the classes, methods, and fields
declared in the current Dart file. For example, the outline for the
Sunflower sample’s <code>sunflower.dart</code> file looks something
like <xref linkend="sunflower-outline" />.</para>
<figure float="0" id="sunflower-outline">
<title>The Outline panel for the Sunflower sample</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0404.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
<para>You can reduce the size of the list by typing one or more
characters. For example, if you type <emphasis
role="bold">c</emphasis>, only the <emphasis
role="bold">center</emphasis> variables appear.</para>
<para>If you choose an item from the list—for example, <emphasis
role="bold">centerX</emphasis>— the editor scrolls to its
declaration.</para>
<para>Alternatively, add a more permanent outline view by choosing
<emphasis role="bold">Tools > Outline</emphasis>.</para>
</sect4>
<sect4 id="callers-view">
<title>Finding where an API is used</title>
<para>Use the Callers view to find where an API item—such as a
function—is used. To bring up the view, click the item’s name,
right-click, and choose <emphasis role="bold">Find
Callers</emphasis> (or type Ctrl+Alt+H).</para>
<para>To see how a caller itself is called, click its arrow in the
Callers view. For example, <xref linkend="clock-callers" /> shows
that two functions call the <literal>updateTime()</literal>
method.</para>
<figure float="0" id="clock-callers">
<title>The Callers view</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0405.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
<para>Once the Callers view is visible, you can click the Show
Callee Hierarchy button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/callees.gif" width="0.12in" />
</imageobject>
</inlinemediaobject> to make the view show
<emphasis>callees</emphasis>—the functions called by the function
you’re inspecting.</para>
</sect4>
</sect3>
<sect3 id="ch04-editor-edit-refactor">
<title>Refactoring</title>
<para>To change the name of an item throughout your code, put the
cursor within (or double-click) the item’s name in the Edit view, and
choose <emphasis role="bold">Edit > Rename...</emphasis> or
right-click and choose <emphasis
role="bold">Rename...</emphasis>.</para>
<para>You can rename almost anything—local variables, function
parameters, fields, methods, types, top-level functions, library
prefixes, top-level compilation units, and more. An example of
renaming a top-level compilation unit is changing the name of a file
that’s sourced by a library.</para>
</sect3>
<sect3 id="ch04-editor-edit-searching">
<title>Searching</title>
<para>The search field at the upper right of the Dart Editor window is
an easy way to go directly to:</para>
<itemizedlist>
<listitem>
<para>Types</para>
</listitem>
<listitem>
<para>Files</para>
</listitem>
<listitem>
<para>Text inside of files</para>
</listitem>
</itemizedlist>
<para>The scope of a text search is every file in your Files view.
Results for text searches come up in a Search view. Within that view,
double-click a file to see it in the Edit view. All occurrences of the
search string in the Edit view are highlighted.</para>
</sect3>
</sect2>
<sect2 id="ch04-editor-run">
<title>Running Apps</title>
<para>To run any Dart app, click Dart Editor’s Run button
<inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/run.png" width="0.12in" />
</imageobject>
</inlinemediaobject> while any file in that app is selected. If you’re
working on a web app, Dart Editor brings up a browser window that
displays the app’s HTML page, with the app’s code running inside
it.</para>
<para>When you run a web application using Dart Editor, by default the
app uses the copy of Dartium that’s included in the Dart Editor
download, with the Dart code executing directly in the browser. If your
launch configuration specifies a browser, then Dart Editor uses <link
linkend="ch04-tools-dart2js">dart2js</link> to compile the Dart code to
JavaScript that executes in the browser.</para>
<sect3 id="ch04-editor-run-manage-launch">
<title>Specifying launch configurations</title>
<para>Use <emphasis role="bold">Run > Manage Launches</emphasis> to
specify as many launch configurations as you like.</para>
<para>For web apps, you can specify the following:</para>
<itemizedlist>
<listitem>
<para>HTML file or URL to open</para>
</listitem>
<listitem>
<para>browser (JavaScript only)</para>
</listitem>
<listitem>
<para>arguments to pass to the browser; for example, <literal
moreinfo="none">--allow-file-access-from-files</literal></para>
</listitem>
<listitem>
<para>debugging enabled (Dartium only)</para>
</listitem>
<listitem>
<para>checked mode (Dartium only)</para>
</listitem>
<listitem>
<para>whether to show the browser’s stdout and stderr output
(Dartium only; useful for diagnosing Dartium crashes)</para>
</listitem>
</itemizedlist>
<para>For example, a web app might have a launch configuration for
Dartium and several more configurations corresponding to additional
browsers you want to test.</para>
<para>You can specify the following for command-line apps:</para>
<itemizedlist>
<listitem>
<para>.dart file to execute</para>
</listitem>
<listitem>
<para>arguments to pass to the app</para>
</listitem>
<listitem>
<para>checked mode</para>
</listitem>
<listitem>
<para>heap size</para>
</listitem>
</itemizedlist>
</sect3>
<sect3 id="ch04-editor-run-production-mode">
<title>Running in production mode</title>
<para>By default, apps run in checked mode. To run in production mode
instead, disable checked mode in your app’s launch
configuration:</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>Run your app at least once, so that it has a launch
configuration.</para>
</listitem>
<listitem>
<para>Choose <emphasis role="bold">Run > Manage
Launches</emphasis>, or click the little arrow to the right of the
Run button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/run.png" width="0.12in" />
</imageobject>
</inlinemediaobject>.</para>
</listitem>
<listitem>
<para>In the Manage Launches dialog, find a launch configuration
for your app. Click it if it isn’t already selected.</para>
</listitem>
<listitem>
<para>Unselect <emphasis role="bold">Run in checked
mode</emphasis> (see <xref linkend="production-mode" />).</para>
<figure float="0" id="production-mode">
<title>To run in production mode, unselect checked mode</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0406.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</listitem>
<listitem>
<para>Click <emphasis role="bold">Apply</emphasis> to save your
change, or <emphasis role="bold">Run</emphasis> to save it and run
your app.</para>
</listitem>
</orderedlist>
<para>For details about checked mode and production mode, see <xref
linkend="ch02-runtime-modes" />.</para>
</sect3>
<sect3 id="ch04-editor-run-non-dartium">
<title>Specifying a browser</title>
<para>To specify the browser in which the your app runs:</para>
<orderedlist continuation="restarts" inheritnum="ignore">
<listitem>
<para>Choose <emphasis role="bold">Run > Manage
Launches</emphasis>, or click the little arrow to the right of the
Run button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/run.png" width="0.12in" />
</imageobject>
</inlinemediaobject>. The Manage Launches dialog appears (see
<xref linkend="manage-launches" />). On the left side of the
dialog is a list of all launch configurations that you’ve created
or that were automatically created for you. On the right is
information about the currently selected launch
configuration.</para>
<figure float="0" id="manage-launches">
<title>The Manage Launches dialog</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0407.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</listitem>
<listitem>
<para>In the Manage Launches dialog, click the Create new launch
button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/run-create.png" width="0.12in" />
</imageobject>
</inlinemediaobject>, and choose <emphasis role="bold">New web
launch: JavaScript</emphasis>.</para>
<figure float="0" id="new-web-launch-menu">
<title>Creating a new web launch</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0408.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</listitem>
<listitem>
<para>Enter configuration information:</para>
<itemizedlist>
<listitem>
<para>Give the configuration a name that identifies the app,
the browser, and anything else important.
<emphasis>Example:</emphasis> HelloWeb.html in Firefox</para>
</listitem>
<listitem>
<para>Specify the HTML file or URL to open.
<emphasis>Example:</emphasis> /HelloWeb/HelloWeb.html</para>
</listitem>
<listitem>
<para>Unless you want to use the default system browser,
unselect <emphasis role="bold">Use default system
browser</emphasis> and specify the location of the browser you
want to use. <emphasis>Example:</emphasis>
/Applications/Firefox.app</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Click <emphasis role="bold">Apply</emphasis> to save your
changes, or <emphasis role="bold">Run</emphasis> to save your
changes and launch the app.</para>
</listitem>
</orderedlist>
<para>Now that you’ve set up the launch, you can choose it any time
from the Run button <inlinemediaobject>
<imageobject>
<imagedata fileref="figs/web/run.png" width="0.12in" />
</imageobject>
</inlinemediaobject>. Your app will be automatically compiled to
JavaScript each time you run it.</para>
</sect3>
</sect2>
<sect2 id="ch04-editor-debug">
<title>Debugging Apps</title>
<para>You can debug both command-line and web apps with Dart Editor.
Debugging must be enabled in your launch configuration (which it is, by
default).</para>
<para>Some tips for debugging:</para>
<itemizedlist>
<listitem>
<para>Set breakpoints by double-clicking in the left margin of the
Edit view.</para>
</listitem>
<listitem>
<para>Use the Debugger view to view your app’s state and control its
execution. By default, the Debugger view is to the right of the Edit
view.</para>
</listitem>
<listitem>
<para>To see the values of variables, mouse over the variable or
look in the Debugging view’s Call Stack.</para>
</listitem>
<listitem>
<para>Because everything in Dart is an object and operators are
really methods, you’ll probably use Step Return (F7) more than you’d
expect to climb out of Dart libraries.</para>
</listitem>
<listitem>
<para>To debug web apps, you use Dart Editor with Dartium (<xref
linkend="debugger" />). While you’re debugging, Dart Editor takes
the place of the Dartium console. For example, Dart Editor displays
the output of<literal> print()</literal> statements.</para>
</listitem>
</itemizedlist>
<figure float="0" id="debugger">
<title>Using Dart Editor to debug the Sunflower sample app</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0409.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</sect2>
<sect2 id="ch04-editor-dart2js">
<title>Compiling to JavaScript</title>
<para>You might not need to do anything to compile your code to
JavaScript. When you run an app using a launch configuration that <link
linkend="ch04-editor-run-non-dartium">specifies a browser</link>, Dart
Editor automatically compiles the app to JavaScript before executing it
in the browser.</para>
<para>However, you can also compile Dart code to JavaScript without
running the app. Just choose <emphasis role="bold">Tools > Generate
JavaScript</emphasis>. Another option is using dart2js from the command
line (see <xref linkend="ch04-tools-dart2js" />).</para>
</sect2>
<sect2 id="ch04-editor-more">
<title>Other Features</title>
<para>Dart Editor has many additional features, including doc
generation, customization, and keyboard alternatives.</para>
<sect3 id="ch04-editor-more-dartdoc">
<title>Generating documentation with dartdoc</title>
<para>Use the <emphasis role="bold">Tools > Generate
Dartdoc</emphasis> command to generate HTML documentation from Dart
code. For information on supplying text for the documentation, see
<xref linkend="comments-doc" />.</para>
</sect3>
<sect3 id="ch04-editor-more-preferences">
<title>Customizing Dart Editor</title>
<para>You can customize the editor’s font, margins, key bindings, and
more using the Preferences dialog. To bring up the dialog, choose
<emphasis role="bold">Tools > Preferences</emphasis> (on Mac:
<emphasis role="bold">Dart Editor > Preferences</emphasis>).</para>
<para>You can also customize which views you see in Dart Editor, as
well as their size and position. To add views, use the <emphasis
role="bold">Tools</emphasis> menu. To remove a view, click its
<emphasis role="bold">X</emphasis>. To move a view, drag its tab to a
different position, either within or outside of the Dart Editor
window. To resize a view, drag its edges.</para>
</sect3>
<sect3 id="ch04-editor-more-keys">
<title>Keyboard alternatives</title>
<para>To get a list of all keyboard alternatives, choose <emphasis
role="bold">Help > Key Assist</emphasis> (<xref
linkend="key-assist" />).</para>
<figure float="0" id="key-assist">
<title>Help > Key Assist</title>
<mediaobject>
<imageobject role="web">
<imagedata fileref="figs/web/daur_0410.png" format="PNG" />
</imageobject>
</mediaobject>
</figure>
</sect3>
</sect2>
</sect1>
<sect1 id="ch04-tools-dartium">
<title>Dartium: Chromium with the Dart VM</title>
<para>This section tells you how to get and use Dartium, a Chromium-based
browser that includes the Dart VM. This browser can execute Dart web apps
directly, so you don’t have to compile your code to JavaScript until
you’re ready to test on other browsers.</para>
<warning>
<para>This browser is a technical preview, and it might have security
and stability issues. <emphasis role="bold">Do not use Dartium as your
primary browser!</emphasis></para>
</warning>
<sect2 id="ch04-dartium-download">
<title>Downloading and Installing the Browser</title>
<para>If you have an up-to-date version of Dart Editor, you already have
Dartium.</para>
<para>If you don’t have Dart Editor or want a different version of
Dartium, you can get it separately from the <ulink
url="http://www.dartlang.org/downloads.html">Downloads page.</ulink> The
Dartium binary does expires after a while. When that happens, you’ll
need to download a new copy if you want to continue using
Dartium.</para>
<para>You don’t usually need to do anything special to install Dartium:
just unarchive the ZIP file. If you want Dart Editor to launch a
particular copy of Dartium, then put that copy inside the <literal
moreinfo="none">dart-sdk</literal> directory of your Dart installation
directory (see <xref linkend="ch01-editor-download" />), replacing the
original copy of Chromium.</para>
</sect2>
<sect2 id="ch04-dartium-launch">
<title>Launching the Browser</title>
<para>To launch Dartium, navigate to its directory in your finder, and
double-click the Chromium executable file. Or use Dart Editor as
described in <xref linkend="ch04-editor-run" /> or the command line as
described in <xref linkend="ch04-dartium-command" />.</para>
<warning>
<para><emphasis>If you already use Chromium:</emphasis> If another
version of Chromium is open, then you could have a profile conflict.
To avoid this, you can open Dartium or Chromium from the command line
with the <ulink
url="http://www.chromium.org/user-experience/user-data-directory">--user-data-dir
flag.</ulink></para>
</warning>
</sect2>
<sect2 id="ch04-dartium-bug">
<title>Filing Bugs</title>
<para>If you find a bug in Dartium, create an issue in the Dart project
and use the <ulink
url="http://code.google.com/p/dart/issues/entry?template=Defect%20report%20for%20Dartium">Dartium
bug template.</ulink></para>
</sect2>
<sect2 id="ch04-dartium-linking">
<title>Linking to Dart Source</title>
<para>Use a script tag with a type <literal
moreinfo="none">application/dart</literal> to link to your Dart source
file. Example:</para>
<programlisting format="linespecific"><remark>lang-html
</remark><!DOCTYPE html>
<html>
<body>
<script type="application/dart" src="app.dart"></script>
<!-- Bootstraps the Dart VM and handles non-Dart browsers. -->
<script src="packages/browser/dart.js"></script>
</body>
</html></programlisting>
<note>
<para>Dart Editor automatically adds both the <literal
moreinfo="none">application/dart</literal> script tag and the
bootstrap JavaScript tag into the project’s main HTML file.</para>
</note>
</sect2>
<sect2 id="ch04-dartium-detect">
<title>Detecting Dart Support</title>
<para>The above example uses a bootstrap script that takes care of
turning on the Dart VM, as well as compatibility with non-Dart browsers.
Instead of using the <literal moreinfo="none">dart.js</literal> script,
you can manually include the necessary JavaScript code.</para>
<para>To start the Dart VM, use the JavaScript function <literal
moreinfo="none">navigator.webkitStartDart()</literal>. For
example:</para>
<programlisting format="linespecific"><remark>lang-js
</remark>// In JavaScript code:
if (!navigator.webkitStartDart) {
// No native Dart support.
window.addEventListener("DOMContentLoaded", function (e) {
// ...Fall back to compiled JS...
}
}, false);
} else {
navigator.webkitStartDart();
}</programlisting>
</sect2>
<sect2 id="ch04-dartium-command">
<title>Launching from the Command Line</title>
<para>Because Dartium is based on Chromium, all <ulink
url="http://www.chromium.org/developers/how-tos/run-chromium-with-flags">Chromium
flags</ulink> should work. In some cases, you might want to specify
Dart-specific flags so that you can tweak the embedded Dart VM’s
behavior. For example, while developing your web app, you might want the
VM to perform as many checks as possible. To achieve that, you can
enable checked mode (the VM’s --enable-type-checks flag) and assertion
checks (--enable-asserts flag).</para>
<para>On Linux, you can specify flags by starting Dartium as
follows:</para>
<screen format="linespecific"><remark>lang-sh
</remark>DART_FLAGS='--enable-type-checks --enable-asserts' <replaceable>path</replaceable>/chrome</screen>
<para>On Mac:</para>
<screen format="linespecific"><remark>lang-sh
</remark>DART_FLAGS='--enable-type-checks --enable-asserts' \
<replaceable>path</replaceable>/Chromium.app/Contents/MacOS/Chromium</screen>
<para>Or (also on Mac):</para>
<screen format="linespecific"><remark>lang-sh
</remark>DART_FLAGS='--enable-type-checks --enable-asserts' \
open <replaceable>path</replaceable>/Chromium.app</screen>
<note>
<para>You can see the command-line flags and executable path of your
current Chromium-based browser by going to <literal
moreinfo="none">chrome://version</literal>.</para>
</note>
</sect2>
</sect1>
<sect1 id="ch04-tools-dart2js">
<title>dart2js: The Dart-to-JavaScript Compiler</title>
<para>You can use the <emphasis>dart2js</emphasis> tool to compile Dart
code to JavaScript. <link linkend="ch04-editor-dart2js">Dart Editor</link>
uses dart2js behind the scenes whenever Dart Editor compiles to
JavaScript. This section tells you how to use dart2js on the command line.
It also give tips on debugging the JavaScript that dart2js